Following is the complete process of uploading/creating a file on the SharePoint server using the Client Object Model. The following process should also be implied for some of the ListItems. Traditionally, we use ListItemCreationInformation class to create a ListItem. However, if your List is a
- Web Page Library
- Picture Library
then also you have to use the below process to create their ListItem otherwise, you’ll get the Microsoft.SharePoint.Client.ServerException, use the SPFileCollection.Add() to create file. This is one of such example where I am creating a ListItem for the Web Page Library List
using System; using System.Collections.Generic; using IO = System.IO; using System.Linq; using Microsoft.SharePoint.Client; using SP = Microsoft.SharePoint.Client;
string siteUrl = "http://siteUrl"; SP.ClientContext ctx = new SP.ClientContext(siteUrl); SP.List list = ctx.Web.Lists.GetByTitle("ListTitle");
byte[] bytes = IO.File.ReadAllBytes(strFilePath); //strFilePath is the full path of the file that needs to be uploaded IO.MemoryStream mStream = new IO.MemoryStream(bytes); SP.FileCreationInformation fileInfo = new SP.FileCreationInformation(); fileInfo.ContentStream = mStream; fileInfo.Overwrite = true; fileInfo.Url = "https://siteUrl/Subsite1/Subsite2/SitePages/TestPage.aspx"; SP.File file = list.RootFolder.Files.Add(fileInfo); list.Update(); SP.ListItem listItem = file.ListItemAllFields; //If you have the MetaInfo that you want to restore listItem["MetaInfo"] = ltItemEntity.FieldValuesAsTexts["MetaInfo"]; listItem.Update(); ctx.ExecuteQuery();
To update large files, up to 2GB, check out my other blog at, https://realmpksharepoint.wordpress.com/2014/04/29/upload-large-files-to-the-sharepoint-documentlibrary-using-rpc-from-a-desktop-application-c/.
Also, you can visit the following post to upload files up to 10GB to SharePoint Online,
https://realmpksharepoint.wordpress.com/2016/07/22/upload-large-files-to-sharepoint-online/.
[…] Here’s the link to upload smaller files using CSOM, https://realmpksharepoint.wordpress.com/2014/01/19/upload-a-file-to-sharepoint-using-client-object-m…. […]
LikeLike
Could you please share this code in JQuery?
LikeLike