Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to connect to a SharePoint 2010 site using the .NET Client Object Model. When I run test code from a windows console app that I have written, I have no problems. When I try to run the same code through the process it will be used in (a scheduled task) I receive the above error.

I have used the following code the connect to the site in both cases:-

using (SP.ClientContext oContext = new SP.ClientContext(SharePointUrl))
{
    oContext.Credentials = new NetworkCredential("user", "password", "domain");
    SP.Web oWeb = oContext.Web;
    oContext.Load(oWeb);

    SP.List caseList = oWeb.Lists.GetByTitle("MyList");
    oContext.Load(caseList);
    oContext.ExecuteQuery();
}

When I ran the code from the scheduled task without the credential info, I received an Unauthorised error (which I would expect) but am at a loss as to why the code is failing now I have provided the credentials. When I run the console app with the credential info, I can access the site data.

Does anybody have any ideas? Thanks.

EDIT By changing the URL of the SharePoint site to be one not installed locally on my development machine I have lost the error message above and I now get the error "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.". Again, testing on my console app works fine.

share|improve this question
add comment

2 Answers

I had a similar problem regarding the connection closed when I was trying to retrieve a lot of items. I used row limit in a CAML query to retrieve some instead of all.

using (SP.ClientContext oContext = new SP.ClientContext(SharePointUrl))
{
    oContext.Credentials = new NetworkCredential("user", "password", "domain");
    SP.Web oWeb = oContext.Web;
    oContext.Load(oWeb);

    SP.List caseList = oWeb.Lists.GetByTitle("MyList");
    oContext.Load(caseList);
    oContext.ExecuteQuery();

CamlQuery query = new CamlQuery();
                    query.ViewXml = @"<View Scope= 'RecursiveAll'> 
<Query>
</Query>
<RowLimit>1000</RowLimit>
</View>";

ListItemcollection oListItems = caseList.GetItems(query);
oContext.Load(oListItems);
ocontext.ExequteQuery();

}
share|improve this answer
add comment

You can also just set the RequestTimeout property of your ClientContext object to be longer than the standard 180000 milliseconds (3 minutes)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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