How to iterate SharePoint List Fields using SharePoint web service?
Hi All,
Here is some code snippet that will help you out to get rid of it.
Here is a scenario where I need to iterate fields and populate these fields in dropdown list for selected SharePoint list in dropdown list.
Reference from link
http://www.codeguru.com/csharp/csharp/cs_webservices/tutorials/print.php/c8805__2/private XmlNodeList RunXPathQuery(XmlNode XmlNodeToQuery, string XPathQuery)
{
// load the complete XML node and all its child nodes into an
// XML document
XmlDocument Document = new XmlDocument();
Document.LoadXml(XmlNodeToQuery.OuterXml);
// all the possible namespaces used by SharePoint and a randomly
// choosen prefix
const string SharePointNamespacePrefix = "sp";
const string SharePointNamespaceURI =
"http://schemas.microsoft.com/sharepoint/soap/";
const string ListItemsNamespacePrefix = "z";
const string ListItemsNamespaceURI = "#RowsetSchema";
const string PictureLibrariesNamespacePrefix = "y";
const string PictureLibrariesNamespaceURI =
"http://schemas.microsoft.com/sharepoint/soap/ois/";
const string WebPartsNamespacePrefix = "w";
const string WebPartsNamespaceURI =
"http://schemas.microsoft.com/WebPart/v2";
const string DirectoryNamespacePrefix = "d";
const string DirectoryNamespaceURI =
"http://schemas.microsoft.com/sharepoint/soap/directory/";
const string DataRowSetNameSpacePrefix = "rs";
const string DataRowSetNameSpaceURI = "urn:schemas-microsoft- com:rowset";
// now associate with the xmlns namespaces (part of all XML
// nodes returned from SharePoint), a namespace prefix that
// we then can use in the queries
XmlNamespaceManager NamespaceMngr =
new XmlNamespaceManager(Document.NameTable);
NamespaceMngr.AddNamespace(SharePointNamespacePrefix,
SharePointNamespaceURI);
NamespaceMngr.AddNamespace(ListItemsNamespacePrefix,
ListItemsNamespaceURI);
NamespaceMngr.AddNamespace(PictureLibrariesNamespacePrefix,
PictureLibrariesNamespaceURI);
NamespaceMngr.AddNamespace(WebPartsNamespacePrefix,
WebPartsNamespaceURI);
NamespaceMngr.AddNamespace(DirectoryNamespacePrefix,
DirectoryNamespaceURI);
NamespaceMngr.AddNamespace(DataRowSetNameSpacePrefix, DataRowSetNameSpaceURI);
// run the XPath query and return the result nodes
return Document.SelectNodes(XPathQuery, NamespaceMngr);
}
When you click button you need to populate fields of “MySharePoint List” to dropdown list.
protected void Button1_Click(object sender, EventArgs e)
{
//List Web service instance.
ListService.Lists listService = new ListService.Lists();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("admin", "admin");
System.Net.CredentialCache cache=new System.Net.CredentialCache();
cache.Add(new Uri("http://localhost:80"),"NTLM",cred);
listService.Credentials=cache;
listService.Url = "http://localhost:80/_vti_bin/lists.asmx";
//Provide SharePoint List name
XmlNode list = listService.GetList("MySharePointList");
//here “//sp:Field” is XPath query we need to pass.
XmlNodeList nodes = RunXPathQuery(list, "//sp:Field");
foreach (XmlNode node in nodes)
{
try
{
bool hidden = false;
bool ReadOnly = false;
string ID = node.Attributes["ID"].Value;
string Name = node.Attributes["Name"].Value;
try{hidden=Convert.ToBoolean(node.Attributes["Hidden"].Value);}catch{}
try { ReadOnly = Convert.ToBoolean(node.Attributes["ReadOnly"].Value); }
catch { }
if (!string.IsNullOrEmpty(ID) && !string.IsNullOrEmpty(Name))
{
if(!hidden || !ReadOnly)
//ddlField is dropdown list which we need to populate.
ddlField.Items.Add(new ListItem(Name, ID));
}
}
catch { }
}
}