The remote sever returned an error: (401) Unauthorized

When I am trying to get the list items from the SharePoint 2013 list buy using the CSOM, it gives the Unauthorized Error. I have created the Priveder hosted App. I have the owner permission in the site. What are the possible troubleshoot steps.

My code snippet is below

Uri

hostWeb;

protected void Page_Load(object sender, EventArgs e)

{

// The following code gets the client context and Title property by using TokenHelper.

// To access other properties, you may need to request permissions on the host web.

hostWeb =

new Uri(Request.QueryString["SPHostUrl"]);

using (var clientContext = new ClientContext(hostWeb))//TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))

{

clientContext.Load(clientContext.Web, web => web.Title);

clientContext.ExecuteQuery();

ShowCurrentloggedInUser(clientContext);

BindBusinessUnits(clientContext);

}

}

private void BindBusinessUnits(ClientContext clientContext)

{

//Below line is optional, if you are getting error related to proxy or authentication use this

clientContext.AuthenticationMode =

ClientAuthenticationMode.Anonymous;

List listBU = clientContext.Web.Lists.GetByTitle("BUList");

CamlQuery cQuery = new CamlQuery();

cQuery.ViewXml =

@"<Query><Where><Neq><FieldRef Name='Title' /><Value Type='Text'>BU</Value></Neq></Where></Query>";

Microsoft.SharePoint.Client.

ListItemCollection BUCollection = listBU.GetItems(cQuery);

clientContext.Load(BUCollection);

clientContext.ExecuteQuery();

//var listData = BUCollection.ToList().Select(i => i["Title"]).Distinct();

DataTable dTable = new DataTable();

dTable.Columns.Add(

"BU");

// To format text in Title Case

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

TextInfo textInfo = cultureInfo.TextInfo;

for (int iCntr1 = 0; iCntr1 < BUCollection.Count; iCntr1++)

{

dTable.Rows.Add(dTable.NewRow());

if (BUCollection[iCntr1]["BU"] != null)

{

dTable.Rows[dTable.Rows.Count - 1][

"BU"] = textInfo.ToTitleCase(BUCollection[iCntr1]["BU"].ToString());

}

}

if (dTable.Rows.Count > 0)

{

ddlBU.DataValueField =

"BU";

ddlBU.DataTextField =

"BU";

ddlBU.DataSource = dTable;

ddlBU.DataBind();

ddlBU.Items.Insert(0,

"Select BU");

}

}


February 23rd, 2015 2:32am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics