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!