You must Sign In to post a response.
  • Category: Sharepoint

    How to Rename the uploaded document name in SharePoint document library using the client

    Hi All,

    Give me one suggestion or sample code for the given requirement.

    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?




    Thnaks In Advaces.
    Chitaranjan
  • #760150
    Hello Chitranjan Mallick,

    Refer the below code :

    Method 1 :

    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();


    Method 2 :

    SPSite oSite = new SPSite ("http://<sitename>/");
    SPWeb oWeb = oSite.OpenWeb();
    SPList oList = oWeb.Lists["Shared Documents"];
    SPListItem oListItem = oList.Items[0]; //taking the first list item
    oListItem.File.CheckOut();
    oListItem["Name"] = "xyz";
    oListItem.Update();
    oListItem.File.CheckIn("file name has been changed");
    oWeb.Dispose();


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #760190
    Hai Chitaranjan Mallick,
    To rename a document which is in document library, first you need get all the document and search your document. Once you get your document rename it and then again update the document list.
    Below is sample code to do the same:

    SPSite site = new SPSite("http://abc.com/"); // go to the root level
    SPWeb web = site.OpenWeb(); // go to your website
    SPList list = web.Lists["Shared Documents"]; // document list
    SPListItem listItem = list.Items[0]; // get the file
    listItem.File.CheckOut(); // checkout the file
    listItem["Name"] = "newName"; // new name for the file
    listItem.Update(); // update the file
    listItem.File.CheckIn("updated");// checkin the file
    web.Dispose(); // clear memory for the website

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments