The remote server returned an error: (403) Forbidden on ExecuteQuery

Question

biconix on Thu, 03 Jan 2013 03:29:56


(I am a CRM developer and have very litle expereince with Sharepoint development)

I am writing a CRM plugin that needs to update a List in Sharepoint. the application will not run an a Sharepoint Server but in IIS. Thus there is no user interface.

I am able to view and edit the list in a browser but I am not able to retreive the list programatically. I get an error: The remote server returned an error: (403) Forbidden

The code is very simple at this point:

using (ClientContext context = new ClientContext(SPSITE))

                {

                    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(USER, PASSWORD, DOMAIN);

                    context.Credentials = credentials;

                    List list = context.Web.Lists.GetByTitle(SPLIST);

                    context.ExecuteQuery(); <= error occurs here

                    // Now update the list.

                }

I probably should point out that the site uses ADFS, but I am able to login using Windows Authentication (via browser).

WHat am I leaving out that is preventing me from retreiving the list?

thanks





Replies

Hemendra Agrawal on Thu, 03 Jan 2013 06:15:16


Hi,

It seems that USER is not having permission to access list so have you try to open sharepoint site by login as this user? Please try and verify.

Also look at this blog, which indicate to getauthenticatedcontext method:

http://razirais.wordpress.com/2011/05/19/how-to-fix-sharepoint-online-403-forbidden-error-while-downloading-files-using-client-object-model/

Let us know your result

Kranthi Kumar Amaravadi on Thu, 03 Jan 2013 07:17:03


Hi,

You can also use web services to read list items

http://sarangasl.blogspot.com.au/2009/12/sharepoint-list-web-service.html

Gnanasekhar on Thu, 03 Jan 2013 10:23:11


Hi,

a small example to get the latest list item using ClientContext,

string oldDocFile = string.Empty;
            string newDocFile = string.Empty;
            string uri = "http://SPUrl";
            using (ClientContext clientContext = new ClientContext(new Uri(uri)))
            {
             //   clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Net.NetworkCredential cred = new System.Net.NetworkCredential("username", "password", "domain");
                clientContext.Credentials = cred;
                // SPDashBoardBL objSPDashboardBal = new SPDashBoardBL();
                Web web = clientContext.Web;

                //  Log Entry
                //  objSPDashboardBal.Get();
                List list = web.Lists.GetByTitle("ListName");
                CamlQuery query = new CamlQuery();
                query.ViewXml = @"<View><Query><OrderBy><FieldRef Name='Modified' Ascending='True' /></OrderBy></Query></View>";

                ListItemCollection listItems = list.GetItems(query);
                clientContext.Load(listItems, li => li.Include(i => i["Title"], i => i["Modified"], i => i["FileLeafRef"]));
                clientContext.ExecuteQuery();

                foreach (ListItem item in listItems)
                {
                    oldDocFile = item["FileLeafRef"].ToString();
                }

                newDocFile = oldDocFile;
            }

Hope this helps

manish parab on Thu, 03 Jan 2013 11:17:33


try

removing below lines

System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(USER, PASSWORD, DOMAIN);

context.Credentials = credentials;

please let me know if it works

regards

manish

biconix on Fri, 04 Jan 2013 02:02:28


Thanks for your reply, but I still get the same error on the ExecuteQuery

biconix on Fri, 04 Jan 2013 02:02:44


Nope, same error

biconix on Fri, 04 Jan 2013 02:29:50


the problem with this solution is that it requires a window to popup. the application I need to write is an unattended service. there can be no user interface

Srinu Tamada on Fri, 04 Jan 2013 04:23:33


1. If you are getting 403 then current user don't have access to the particular list.

2. If the user has access and still you are not able to retrive then user credentials are not recognizing at SP end.

3. To retrive the data you can use list service, its simple and best way.

4. If you don't want to use list service then debug and check web url, list value and credential details to understand more.

biconix on Fri, 04 Jan 2013 04:44:57


yes, I think I will have to use the list web service. I now need to learn how to do that....

thanks

Hemendra Agrawal on Fri, 04 Jan 2013 11:06:09


Hi,

Then use lists.asmx web service where you can pass same network credential but this user should have at least read access in site. However above solution should also work if everything is set properly but i don't understand where is problem as we have very few details about your requirement.

Now try with SP web service by following below blog:

http://jeanpaulva.com/2012/06/16/sharepoint-2010-web-services/

http://blogs.msdn.com/b/pranab/archive/2008/11/24/sharepoint-2007-moss-wss-using-lists-asmx-getlistitems.aspx

Let us know your result

biconix on Wed, 09 Jan 2013 14:46:28


I still get the same error

Sachin Dagar on Thu, 14 Feb 2013 18:11:26


Hi Biconix,

If you are still facing same issue then try this post

http://blogs.msdn.com/b/cjohnson/archive/2011/05/14/part-2-headless-authentication-with-sharepoint-online-and-the-client-side-object-model.aspx

biconix on Fri, 15 Feb 2013 02:55:32


it turns out the problem was with ADFS. it was probably not configured properly. when they disabled ADFS everything work as expected