2

I have one interesting problem, but I don't know why this happend. Generally I: 1) try get content from file (succesful result) 2) then I rename file in sharepoint (succesful result) 3) try get content of the renamed file (fail)

in code it looks like:

First get content:

public Stream GetFileContent(int fileId)
{
    var item = _list.GetItemById(fileId);
    var fileStream = item.File.OpenBinaryStream();
    _clientContext.ExecuteQuery();
    return fileStream.Value;
}

Then rename:

public void Rename(int id, string newName)
{
    var item = _list.GetItemById(id);
    item["Title"] = newName;
    item["FileLeafRef"] = newName;

    try
    {
        var file = item.File;
        item.Update();
        _clientContext.ExecuteQuery();
    }
    catch (ServerException ex)
    {

    }
}

And for this code after the second call of GetFileContent I have an exception [Microsoft.SharePoint.Client.ServerException] = {"File Not Found."}

==========================================================================

After some investigations I found another way how to rename the file and added Checkout/Checkin commands:

try

        {
        var file = item.File;
        file.CheckOut();
        item.Update();
        file.CheckIn("file name has been changed", CheckinType.MajorCheckIn);
        _clientContext.ExecuteQuery();
    }

But in this case a exception: Message = "The file \"https://my.sharepoint.com/mynewsupersite/Shared Documents/document.docx\" is checked out for editing by i:0#.f|membership|email@email.com"... stuff like that

and renaming doesn't work corrrectly anymore.

I'm new in the sharepoint programming, so can you please explain where I'm wrong and give an idea how to fix it ?

Big thanks

  • Is it problem with renaing or with getting content ? – Vitaly Oct 10 '14 at 12:01
  • I wish I had an answer, but the best I could was to upvote your question. I too have suffered this phenomenon, when using a workflow to rename a file. The rename is successful, but SharePoint forever uses the original name and then of course dies when trying to intereact with the file. I do hope you find an answer to this. – Alan M Oct 15 '14 at 16:18
1

Seems like my problem is fixed. I added .Load() for a list item in the Rename method:

try
        {
            item["Title"] = newName;
            item["FileLeafRef"] = newName;
            item.Update();
            _clientContext.Load(
                item,
                i => i,
                i => i.FileSystemObjectType,
                i => i.File,
                i => i.File.ListItemAllFields,
                i => i.File.ListItemAllFields.EffectiveBasePermissions,
                i => i.File.ServerRelativeUrl,
                i => i.Folder,
                i => i.Folder.ListItemAllFields,
                i => i.Folder.ListItemAllFields.EffectiveBasePermissions,
                i => i.Folder.ServerRelativeUrl,
                i => i.Folder.Properties,
                i => i.Folder.Folders
            );
            _clientContext.ExecuteQuery();
        }

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.