3

I am having some trouble when trying to us the SharePoint Client Object Model against a SharePoint 2013 installation. I am attempting to write some code which will allow both the upload and download of a file into a Document Library.

I have got the Upload part to work perfectly, using:

using (var context = new ClientContext(this.appSettingsProvider.SharePointServerUrl))
{
    try
    {
        using (var fs = new FileStream(filePath, FileMode.Open))
        {
            Microsoft.SharePoint.Client.File.SaveBinaryDirect(
                        context,
                        string.Format("/{0}/{1}", documentFolder, fileInfo.Name),
                        fs,
                        false);
        }

        return true;
    }
    catch (Exception ex)
    {
        this.logger.Error(ex);
    }

    return false;
    }
}

Where filePath and documentFolder are passed into my function.

However, when it comes to deleting the document, I am using the following code which I found here:

Delete Item in Document Library using Client Object Model?

var returnValue = false;

try
{
    using (var clientContext = new ClientContext(this.appSettingsProvider.SharePointServerUrl))
    {
        var sharePointList = clientContext.Web.Lists.GetByTitle(documentFolder);
        var query = new CamlQuery();
        query.ViewXml = "<View>" + "<Query>"
                                + "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + filePath
                                + "</Value></Eq></Where>" + "</Query>" + "</View>";

        // execute the query
        var listItems = sharePointList.GetItems(query);
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();

        foreach (var listitem in listItems)
        {
            listitem.DeleteObject();
            clientContext.ExecuteQuery();
            returnValue = true;
        }
    }
}
catch (Exception ex)
{
    this.logger.Error(ex);
}

return returnValue;

When I try to run this code, I keep getting back the exception:

Too many automatic redirections were attempted.

Has anyone run into this before? Any ideas on how to correct this? I have found references to a requirement to capture the cookies that are being used in the request, but I couldn't find any clear way on how to implement this.

I ran into a similar issue when I tried to use this code:

https://stackoverflow.com/a/8011028/671491

For checking whether a item already exists in the document library. The thing they have in common is the use of ExecuteQuery.

Any help would be greatly appreciated!

1 Answer 1

0

You are committing one Cardinal Sin of .NET programming; you are deleting items of a collection in a foreach loop. When you delete an item in a loop like this, you are changing the collection that's being iterated, but the foreach loop still attempts to iterate as if the collection was still in its original state, and eventually the enumerator get out of whack and an exception is thrown.

The by-the-book way to delete items in a collection is to use a backwards for-loop, like this:

for (int i = listItems.Count -1; i  <= 0; i--)
{
   listitems[i].DeleteObject();
}
clientContext.ExecuteQuery();
returnValue = true;

I would also move the ExecuteQuery() line outside the loop. You only need to call it and set the return value once.

5
  • thanks for the suggestion! I have implemented your suggestion for the looping through the listItems result set. However, I don't think this is what is causing the problem that I am currently having. It is the first clientContext.ExecuteQuery() that is causing the exception to be thrown, not the second one. Any thoughts on what else could be going on here? Thanks! Feb 15, 2015 at 12:41
  • A quick Google search of the error message leads me to believe this actual code isn't what's causing your error. How are you hosting this code? In a web page? Are you doing any sort of other http requests or authentication inside the page? How is your ClientContext authenticated? Does your code throw the same error when run from a console application? Feb 15, 2015 at 17:53
  • The code that I am running is within a Windows Service. Within the Windows Service, it is using the SharePoint Client Object Model to both add and delete entries from a SharePoint Document Library. The identity that is running the Windows Service is one that has the correct rights to the SharePoint instance to perform these functions. The uploading of the documents works as expected, only the deletion doesn't. So authentication happens via the identity running the Windows Service. Haven't tried from Console Application, something to try though. Feb 15, 2015 at 19:34
  • Did you ever find out what it was, maybe some proxy issue?
    – Shihan
    May 27, 2019 at 11:17
  • Thats not causing the exception nor does this answer help solving it.
    – Mx.
    May 29, 2020 at 9:57

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.