This is an example of how to upload a file with the C# Client Side Object Model (CSOM):
internal static File UploadFile(ClientContext clientContext, Web web, string filePath, Folder folder, string fileName, string title) { string target = folder.ServerRelativeUrl + "/" + fileName; FileCreationInformation fci = new FileCreationInformation(); fci.Overwrite = true; fci.Url = target; fci.Content = System.IO.File.ReadAllBytes(filePath); File uploadedFile = folder.Files.Add(fci); uploadedFile.ListItemAllFields["Title"] = title; uploadedFile.ListItemAllFields.Update(); clientContext.ExecuteQuery(); if (uploadedFile.CheckOutType != CheckOutType.None) { uploadedFile.CheckIn("Initial Upload", CheckinType.MajorCheckIn); } clientContext.Load(uploadedFile); clientContext.ExecuteQuery(); return uploadedFile; }
Note that this method also lets you set a title for the document, as well as the file name, and it checks the document in for you if required.
Very useful code you shared. Thanks for that. Its save my lots of time.
In CSOM can we get the uploaded document full URL?
If yes then request you to pls share the code.
Thanks in Advance
That’s not exactly easy. In part, this could depend on different alternate access mappings – so the same file could appear at different URLs for different zones – e.g. Internet and Intranet.
What you could do is take the site relative URL and use the SPSite.MakeFullUrl() method to get the correct full URL for the current user’s zone:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.makefullurl(v=office.14).aspx