1

I have a ListItem and would like to set the Name field for a file in a doc library. Researching this question I have seen that you can set an SPListItem name like this

SPListItem oListItem = oList.Items[0];
oListItem["Name"] = "xyz";           
oListItem.Update();

I am assuming it's different because I am using ListItem and not SPListItem because when I tried to do that in my solution it says the field Name does not exist. What is the right way to do this with ListItem?

ListItem listItem = uploadFile.ListItemAllFields;
listItem["Name"] = "xyz";
listItem.Update();
1

The field I needed to update was FileLeafRef.

After I implemented that change I was still getting an error message (File Not Found). This was because I was uploading the document to the library and you can't update the name of a file that was used when it was first uploaded into SharePoint. I had to add a method to update the file Name after the file was finished uploading, below is the code from that method.

item["FileLeafRef"] = fileName;
item.Update();
clientContext.ExecuteQuery();
  • Interesting, this didn't work for me. In fact, it ended up that the only way to "rename" a document (e.g. its filename) is the File.MoveTo(...) method. See stackoverflow.com/a/26710865/410753 – sasfrog Sep 7 '18 at 14:03

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.