0

I just recently started to invest some time into CAML. I am trying to learn it. I think I have it down, but when I try executing the query I get a 403 forbidden. Anyone know a way to fix this. error comes a line 21

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace SharepointConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "https://fufu.sharepoint.com/fpattegrosso";
            ClientContext clientContext = new ClientContext(siteUrl);
            Console.WriteLine("connected");
            Console.ReadLine();
            SP.List oList = clientContext.Web.Lists.GetByTitle("Log");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/><Value Type='Number'>0</value></Greq></Where></Query><RowLimiti>100</RowLimit></View>";
                //Entry
            ListItemCollection collListItem = oList.GetItems(query);
            clientContext.Load(collListItem);
            clientContext.ExecuteQuery(); // line 21
            foreach(ListItem item in collListItem)
            {
                Console.WriteLine($"{item["Title"]}");
            }
            Console.Read();
        }
    }
}
1

It seems to be related to permission, therefore, initially would indicate that you will likely need to include your credentials with the client context, as the user will need access to the respective list.

using System;
using System.Net;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace SharepointConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "https://fufu.sharepoint.com/fpattegrosso";
            ClientContext clientContext = new ClientContext(siteUrl);

            // Added Using System.Net ^^ ///////
            NetworkCredential cred = new System.Net.NetworkCredential("username", "password", "domain");
            clientContext.Credentials = cred;

            Console.WriteLine("connected");
            Console.ReadLine();
            SP.List oList = clientContext.Web.Lists.GetByTitle("Log");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/><Value Type='Number'>0</value></Greq></Where></Query><RowLimiti>100</RowLimit></View>";
                //Entry
            ListItemCollection collListItem = oList.GetItems(query);
            clientContext.Load(collListItem);
            clientContext.ExecuteQuery();
            foreach(ListItem item in collListItem)
            {
                Console.WriteLine($"{item["Title"]}");
            }
            Console.Read();
        }
    }
}
  • it did not change anything, Still getting 403 – Franco Pettigrosso Apr 3 '17 at 18:31
  • So, the accepted answer, was your own answer, however, instead of using NetworkCredential, you ended up using SharePointOnlineCredentials. Either way, it appeared to be credential / permission related. So, in short, the differentiation was the sharepoint online use case - either way, yeah, the limiting factor was the ClientContext Credential property. – Quinn Johns Apr 4 '17 at 3:28
0

follow this method in order to properly authenticate yourself to the sharepoint client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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