Wednesday, September 18, 2013

Sharepoint WS - Lists - Get Document ID

Here is the function to get the document data and it will return the document ID in result string.

 private static string GetDocID(string docname)  
     {  
       try  
       {  
         string result="";  
         using (ListWS.Lists listService = new ListWS.Lists())  
         {  
           listService.Url = ListURL;  
           listService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
           listService.Credentials = new System.Net.NetworkCredential(username, password);  
           XmlDocument doc = new XmlDocument();  
           XmlElement query = doc.CreateElement("Query");  
           XmlElement viewFields = doc.CreateElement("ViewFields");  
           XmlElement queryOptions = doc.CreateElement("QueryOptions");  
           //query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +  
           //     "<Value Type=\"Counter\">0</Value></Gt></Where>";  
           //docname = "STAR.BE_CIS_B_My%20%20test%20123.pdf";  
           query.InnerXml = "<Where><Eq><FieldRef Name=\"LinkFilename\" />" +  
                "<Value Type=\"Text\">"+ docname.Replace("%20"," ") + "</Value></Eq></Where>";  
           //query.InnerXml = "<Where><BeginsWith><FieldRef Name=\"LinkFilename\" />" +  
           //     "<Value Type=\"Text\">S</Value></BeginsWith></Where>";  
           viewFields.InnerXml = "";  
           queryOptions.InnerXml = "<ViewAttributes Scope='Recursive'/>";  
           XmlNode nodes = listService.GetListItems("ListName", String.Empty, query, viewFields, string.Empty, queryOptions, null);  
           foreach (System.Xml.XmlNode node in nodes)  
           {  
             if (node.Name == "rs:data")  
             {  
               for (int i = 0; i < node.ChildNodes.Count; i++)  
               {  
                 if (node.ChildNodes[i].Name == "z:row")  
                 {  
                   for (int j = 0; j < node.ChildNodes[i].Attributes.Count; j++)  
                   {  
                     Console.WriteLine(j + "|" + node.ChildNodes[i].Attributes[j].Name + "|" + node.ChildNodes[i].Attributes[j].Value);  
                   }  
                   result = node.ChildNodes[i].Attributes["ows_ID"].Value;  
                 }  
               }  
             }  
           }  
         }  
         return result;  
       }  
       catch (Exception ex)  
       {  
         throw ex;  
       }  
     }  

No comments: