3

I already uploaded the document in SharePoint document library using CSOM.

Now I need to Rename the uploaded file using CSOM, how to achieve this via CSOM? pls look the code below and do the need full...

ClientContext context = new ClientContext(Convert.ToString(ConfigurationManager.AppSettings["DocumentLibraryLink"]));

context.Credentials = CredentialCache.DefaultCredentials;

                Web web = context.Web;
                bool iscontinue = false;

                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(InputFileName);
                newFile.Url = "/" + newFile;

                List docs = web.Lists.GetByTitle(ConfigurationManager.AppSettings["DocumentLibraryName"]);
                string strFilename = InputFileName;

                //rename
                List docs = web.Lists.GetByTitle(“Shared Documents”);
                 docs = web.Lists.GetByTitle(Convert.ToString(ConfigurationManager.AppSettings["DocumentLibraryName"]));

                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
                context.Load(uploadFile);
                 context.ExecuteQuery();

                ListItem item = uploadFile.ListItemAllFields;

                string renamefile = txtfilename.Text.ToString();

                string docTitle = string.Empty;
                item["Title"] = renamefile;

                item.Update();

                 RenameFile(context, strFilename, renamefile);
3

If you want to change file name of an already uploaded document try below code:

Get the uploaded file object using ID/ URL or which ever way suits you, then try this:

ListItem item = uploadedFile.ListItemAllFields;
item["Name"] = "NewName";
item.Update();
// use OverwriteCheckIn type to make sure not to create multiple versions 
uploadFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
ctx.ExecuteQuery();

If you want to change file name of the file while uploading try below code:

 using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
    FileCreationInformation newFile = new FileCreationInformation();
    newFile.ContentStream = fs;
    newFile.Url = newFileName;
    newFile.Overwrite = true;
    List docs = web.Lists.GetByTitle(“Shared Documents”);
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
    ctx.ExecuteQuery();
}
  • hi Unnien, thanks but its working , how to rename the file name in the document library.... – sundhar41 Jul 6 '15 at 9:42
  • Just use the snippet above to rename file in a document library. – Unnie Jul 6 '15 at 9:58
  • can you Please,provide the solution for renaming the file the above code snippet is not working.. please do the need full... – sundhar41 Jul 6 '15 at 11:12
  • Could you post the code in the question – Unnie Jul 6 '15 at 11:17
  • hi unnien, added the code in the question ... – sundhar41 Jul 6 '15 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.