I'm developing a C# console application that queries some items from some lists.
Most lists are in a sub-site, but one of the list I need to query is in the root (or main) site.
Example:
- The sub-site is:
http://prod_01/human-resources/property/
. - The root/main site is:
http://prod_01/
.
In the sub-site http://prod_01/human-resources/property/
I get the information I need by querying certain information from a few lists, but, in the root/main site http://prod_01/
there is a list called Holidays
which I cannot get the list instance.
This is the code I found in this answer for connect to the site via CSOM:
// Variables used in this code:
string url_site = "http://prod_01/human-resources/property/";
string _user = "shpfarm";
string _password = "******";
// CSOM Authentication:
ClientContext clientContext = new ClientContext(url_site);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url_site), "NTLM", new NetworkCredential(_user, _password));
clientContext.Credentials = myCache;
I'm using this code for query the Holidays
list:
try
{
string HolidayDate = DateTime.Today.ToString("yyyy-MM-dd");
string holiday_list_URL = "http://prod_01/human-resources/property/Lists/Holidays/AllItems.aspx";
List lst_Holiday = clientContext.Web.GetList(holiday_list_URL);
clientContext.Load(lst_Holiday);
Console.WriteLine("List called succesfully");
// Query: Get if current date is a holiday:
CamlQuery cmlQueryHoliday = new CamlQuery();
cmlQueryHoliday.ViewXml = string.Format(@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='HolidayDate' />
<Value IncludeTimeValue='TRUE' Type='DateTime'>{0}</Value>
</Eq>
</Where>
</Query>
</View>", HolidayDate);
ListItemCollection colItems = lst_Holiday.GetItems(cmlQueryHoliday);
clientContext.Load(colItems);
clientContext.ExecuteQuery();
}
catch (Exception ex)
{
Console.WriteLine("Error calling list (using URL): " + ex.ToString());
}
But I get this exception (which really doesn't say much about the causes):
in Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
in Microsoft.SharePoint.Library.SPRequest.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder)
in Microsoft.SharePoint.SPWeb.GetList(String strUrl)
in Microsoft.SharePoint.ServerStub.SPWebServerStub.InvokeMethod(Object target, String methodName, XmlNodeList xmlargs, ProxyContext proxyContext, Boolean& isVoid)
in Microsoft.SharePoint.Client.ServerStub.InvokeMethodWithMonitoredScope(Object target, String methodName, XmlNodeList args, ProxyContext proxyContext, Boolean& isVoid)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.InvokeMethod(Object obj, String methodName, XmlNodeList xmlargs, Boolean& isVoid)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPath(XmlElement xe)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.GetObjectFromObjectPathId(String objectPathId)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessInstantiateObjectPath(XmlElement xe)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessOne(XmlElement xe)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.ProcessStatements(XmlNode xe)
in Microsoft.SharePoint.Client.ClientMethodsProcessor.Process()
I tried:
- Use different user credentials.
- Changing the URL of the
Holidays
list fromhttp://prod_01/human-resources/property/Lists/Holidays/AllItems.aspx
tohttp://prod_01/human-resources/property/Lists/Holidays
.
But I get the same exception.
How can I get the list instance by using the URL of Holidays
list? or which alternatives can I use for get the Holidays
list and its items?