2

I am trying to find a simple example in c# or vb that shows how to connect and then pull in a list and display it in a gridview in asp.net.

We are using SharePoint 2013 with Windows Authentication in claims mode. The examples I have found on the Internet do not seem to work.

Hoping someone has a simple example or can point me to one that actually works.

The example I found below keeps giving me this error when I run it:

Unhandled exception at line 23, column 32 in http://localhost:55984/Default.aspx 0x800a1391 - JavaScript runtime error: 'RegisterSod' is undefined occurred

Code

 SharePointList.Lists L = new SharePointList.Lists();
                L.Url = "http://spweb.xyz.com/sites/departments/IT/Lists/IT%20Projects/AllItems.aspx";

                //L.Credentials = new NetworkCredential("Administrator", "YOUR PASSWORD", "YOUR DOMAIN");
                L.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                string listName = "IT Projects";
                string strReturn = "";
                System.Xml.XmlElement ndQuery = xmlDoc.CreateElement("Query");
                System.Xml.XmlElement ndViewFields = xmlDoc.CreateElement("ViewFields");
                System.Xml.XmlElement ndQueryOptions = xmlDoc.CreateElement("QueryOptions");

                ndQuery.InnerXml = "<Where><Eq><FieldRef Name=\"ProjectID\" />" +
                        "<Value Type=\"Single line of text\">ATLAS001</Value>" +
                        "</Eq></Where>";
                ndViewFields.InnerXml = "<FieldRef Name=\"ProjectID\" />";
                ndQueryOptions.InnerXml = "";

                System.Xml.XmlNode nodeListItems = L.GetListItems(listName, null, null, ndViewFields, null, null, null);

                foreach (System.Xml.XmlNode X in nodeListItems)
                {
                    strReturn += X.OuterXml;
                }

                DataSet ds = new DataSet();
                using (StringReader stringReader = new StringReader(strReturn))
                {
                    //initialize our DataSet
                    ds = new DataSet();

                    //load the StringReader to our DataSet
                    ds.ReadXml(stringReader);
                }

                GridView1.DataSource = ds.Tables[1];
                GridView1.DataBind();

Thanks

1

You can read data from SharePoint list in your ASP.NET Using SharePoint CSOM.

Client-Side Object Model (CSOM) is mainly used to build client applications and enable us to access SharePoint Sites that are hosted outside without using web services.


What do you need in order to use CSOM?

You just need to add the below assemblies as a reference to your solution to be able to work with the Client Object Model.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

These assemblies can be found in the 15 Hive folder: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI.

In case your ASP.net solution will not be hosted on a SharePoint server, you should copy these assemblies first from your SharePoint Server to your solution folder then add it as a reference in your solution, otherwise just add the above reference to your solution


To get a SharePoint List and display it in Gridview via CSOM

ClientContext clientContext = new  ClientContext("http://siteurl");
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;   
List list = clientContext.Web.Lists.GetByTitle("list name");  
CamlQuery query = new CamlQuery();  
query.ViewXml = "<View/>";  
ListItemCollection items = list.GetItems(query);  
clientContext.Load(list);  
clientContext.Load(items);  
clientContext.ExecuteQuery();  

 // create a data table
 DataTable LDT= new DataTable();  
   LDT.Columns.Add("ID");  
   LDT.Columns.Add("Title");  
  //fill datatatable
 foreach(ListItem item in items) 
    LDT.Rows.Add(item.Id, item["Title"]);  
  // set datatable as a dataatsource for grid view
   gr.DataSource = LDT;
   gr.DataBind();

For Complete basic operations using SharePoint client library code check

0

One problem is I think your query value type is wrong - it should be Text

 ndQuery.InnerXml = "<Where><Eq><FieldRef Name=\"ProjectID\" />" +
                        "<Value Type=\"Single line of text\">ATLAS001</Value>" +
                        "</Eq></Where>";

should be

 ndQuery.InnerXml = "<Where><Eq><FieldRef Name=\"ProjectID\" />" +
                        "<Value Type=\"Text\">ATLAS001</Value>" +
                        "</Eq></Where>";

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.