Connect to the server. Url is the site URL.
public bool Connect(string Url)
{
bool bConnected = false;
try
{
SPClientContext = GetSPContext(Url);
SPWeb = SPClientContext.Web;
SPClientContext.Load(SPWeb);
SPClientContext.ExecuteQuery();
bConnected = true;
}
catch (Exception ex)
{
bConnected = false;
SPErrorMsg = ex.Message;
}
return bConnected;
}
private ClientContext GetSPContext(string Url)
{
ClientContext SPClientContext = new ClientContext(Url);
SecureString SecSPassWord = new SecureString();
foreach (char c in sPPassword)
{
SecSPassWord.AppendChar(c);
}
SPClientContext.Credentials = new SharePointOnlineCredentials(sPUserName, SecSPassWord);
return SPClientContext;
}
Upload a File to SharePoint
fs means File Stream
sFileName means File Name
sSPSiteRelativeURL means Relative Path
sLibraryName means Document Library Name
fileid means File ID
applicationtype means Type of the Application
public void UploadFile(Stream fs, string sFileName, string sSPSiteRelativeURL, string fileid, string applicationtype)
{
string sDocName = string.Empty;
string folderpath = string.Empty;
try
{
if (SPWeb != null)
{
string sLibraryName = "Test";
var fullFolderUrl = applicationtype + "/" + fileid;
var list = SPWeb.Lists.GetByTitle(sLibraryName);
var rootfolder = list.RootFolder;
//Here you can create the folders in document library.
CreateFolder(fullFolderUrl, rootfolder);
var fUrl = sSPSiteRelativeURL + sLibraryName + "/" + fullFolderUrl + "/" + sFileName;
Microsoft.SharePoint.Client.File.SaveBinaryDirect(SPClientContext, fUrl, fs, true);
}
}
catch (Exception ex)
{
sDocName = string.Empty;
SPErrorMsg = ex.Message;
}
}
Creating a folder and named the file id
fullFolderUrl means Folder Url
parentFolder means parent Folder
CurrentURL means Current URL
public Folder CreateFolder(string fullFolderUrl, Folder parentFolder)
{
string folderpath = string.Empty;
string UploadUrl = sURL;
if (string.IsNullOrEmpty(fullFolderUrl))
throw new ArgumentNullException("fullFolderUrl");
var folderNames = fullFolderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string folderName = folderNames[0];
var curfolder = parentFolder.Folders.Add(folderName);
SPClientContext.Load(curfolder);
SPClientContext.ExecuteQuery();
// If we have sub folders
if (folderNames.Length > 1)
{
var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
return CreateFolder(subFolderUrl, curfolder);
}
return curfolder;
}
2.Use the below method where click on the "upload" button.
private void UploadFilestoSharePoint(Stream fs, string sFileName, Guid id, string filetype)
{
string fileid = id.ToString();
// if you want to specialize the application type
string applicationtype = filetype;
//This is the SharePoint Web url eg "https://test.sharepoint.com"
string UploadUrl = sURL;
Uri uri = new Uri(UploadUrl);
string sSPSiteRelativeURL = uri.AbsolutePath;
SharePointUtil sputil = new SharePointUtil();
bool bbConnected = sputil.Connect(UploadUrl);
if (bbConnected)
{
sputil.UploadFile(fs, sFileName, sSPSiteRelativeURL, fileid, applicationtype);
}
}