Prerequisites
We need to have the AJAX Control toolkit installed.
- Create an ASP.NET web application.
- Add SharePoint DLL References. These references can be found in “C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\15\ISAPI”.
In order to have this dll you need to install SharePoint Client SDK “https://www.microsoft.com/en-us/download/details.aspx?id=35585”
- Now on your web form, you need to drag AjaxFileUpload Controland ScriptManager.
- Now on OnUploadComplete create an Event.
And paste the following code.
- protectedvoidOnUploadComplete(object sender, AjaxFileUploadEventArgs e)
- {
- try
- {
-
- stringfileName = Path.GetFileName(e.FileName);
-
- string path = Path.Combine(Path.GetTempPath(), "_AjaxFileUpload", e.FileId);
-
- Session["FileDirectory"] = path;
-
- ClientContext context = newClientContext(siteUrl);
-
- WeboWebsite = context.Web;
- context.Load(oWebsite);
-
- context.ExecuteQuery();
-
- ListCurrentList = context.Web.Lists.GetByTitle(listName);
- context.Load(CurrentList.RootFolder);
- context.ExecuteQuery();
-
- StringfileURL = (path + "\\" + fileName);
- using(FileStreamfileStream = newFileStream(fileURL, FileMode.Open))
- Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, CurrentList.RootFolder.ServerRelativeUrl.ToString() + "/" + e.FileName, fileStream, true);
- }
- catch (Exception ex)
- {
- lblError.Text = ex.ToString();
- lblError.BackColor = System.Drawing.Color.Red;
- lblError.ForeColor = System.Drawing.Color.White;
- }
- finally
- {
-
- e.DeleteTemporaryData();
- }
- }
- Now run the application.
Files are now uploaded to SharePoint.