3

I am getting an error 409 while trying to write a file in a slide library. All I found on google until now is about wrong paths. But I already wrote about 90 other files in the exact same library until I get to the slide on which the exception is trhown. My guess is that I cannot write the file because another user has opened it, since I get the message that the file is currently in use when I try to open it manually. How can I check that and skip the corresponding file?

try
{
    // Creating the Clientcontext
    ClientContext clientContext = new ClientContext(sharePointAdress);
    SecureString pwd = new SecureString();
    foreach (char c in "pwd".ToCharArray()) pwd.AppendChar(c);

    clientContext.Credentials = new SharePointOnlineCredentials("login", pwd);
    Web oWebsite = clientContext.Web;

    // Collect all lists from Sharepoint.
    ListCollection collList = oWebsite.Lists;
    clientContext.Load(collList);
    clientContext.ExecuteQuery();
    Console.WriteLine("Connected to Sharepoint.");

    // Getting the Presentation.
    List sharedDocumentsList = clientContext.Web.Lists.GetById(id);
    CamlQuery camlQuery = new CamlQuery();
    String queryString = "<Where><Eq><FieldRef Name='FileExtension'><Value Type='text'>pptx</Value></FieldRef></Eq></Where>";    // FileLeafRef is the column "name" on the Sharepoint.
    camlQuery.ViewXml = @queryString;
    ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
    clientContext.Load(sharedDocumentsList);
    clientContext.ExecuteQuery();

    clientContext.Load(listItems, item => item.Include(f => f.File));
    clientContext.ExecuteQuery();
    Console.WriteLine("Opened Document.");

    foreach (var file in listItems)
    {
        clientContext.Load(file);
        clientContext.ExecuteQuery();

        filename = file["FileLeafRef"].ToString();
        Console.WriteLine("Working on " + filename + "..");
        currentID = file["ID"].ToString();
        FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)file["FileRef"]);

        if(file.File.CheckOutType == CheckOutType.None)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                fileInformation.Stream.CopyTo(memoryStream);
                PresentationDocument ppt = PresentationDocument.Open(memoryStream, true);

                // Working on the files..

                memoryStream.Seek(0, SeekOrigin.Begin);
                ClientOM.File.SaveBinaryDirect(clientContext, (string)item["FileRef"], memoryStream, true);
        }
    }
}

I would like to do something similar to where I am checking if the file is checked out.

2
  • Can you share your essential part of code? I need to know how you got file object. Aug 26, 2016 at 12:41
  • Added the Information to the OP Aug 26, 2016 at 12:53

2 Answers 2

2

Here you have to explicitly load the CheckOutType before using it.

Your code to load the list item should look similar to

clientContext.Load(listItem, item => item.File.CheckOutType, item => item.File.CheckedOutByUser, item => item.File.LockedByUser);

in place of

clientContext.Load(listItems, item => item.Include(f => f.File));

Adjust the variable as per requirement.

See here for original post

1
  • I had to adjust your solution slightly: clientContext.Load(listItems, item => item.Include(i => i.File.CheckOutType, i => i.File.CheckedOutByUser, i => i.File.LockedByUser)); But it does seem to work for now. Could you elaborate why? I'd like to understand my error. Aug 26, 2016 at 13:16
1

Change your code as below

foreach (var file in listItems)
{
    if(file.File.CheckOutType == CheckOutType.None)
    {
        // Do things
        clientContext.Load(file);
        clientContext.ExecuteQuery();

        filename = file["FileLeafRef"].ToString();
        Console.WriteLine("Working on " + filename + "..");
        currentID = file["ID"].ToString();
        FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)file["FileRef"]);

    }
}
3
  • Also I don't think you need to Load File again because you have explicitly requested for that as part of your initial listitems query Aug 26, 2016 at 12:50
  • Sadly this doesn't work, since the file is not checked out. It is just locked. Aug 26, 2016 at 12:58
  • Update - Check file.LockId and if it is not empty then it means it is locked Aug 26, 2016 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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