Communicate SharePoint Lists Webservice In Asp.Net Site

This is the first long article I’m writing based on SharePoint technologies. It’s aimed at reasonable .Net developers who are looking to get started developing around SharePoint technologies. By no means is everything I say in the best approach. If you spot something that should be done another way please do mention it in the comments! SharePoint is such a big application and topic we’re all learning as we go along.

A full list of webservice methods for things such as alerts and sites are available in the SharePoint Products and Technologies 2003 SDK. I’ll hopefully cover them all in time, but today we’ll focus on just the list webservice.

You can navigate to the webservice discovery file by typing in :

localhost/sitename/_vti_bin/lists.asmx?wsdl

Here are the methods and their definitions as taken from the SDK:

  • AddAttachment – Adds an attachment to the specified list item in the specified list.
  • AddList – Creates a list in the current site based on the specified name, description, and list template ID.
  • DeleteAttachment – Removes the attachment from the specified list item.
  • DeleteList – Deletes the specified list.
  • GetAttachmentCollection – Returns a list of the URLs for attachments to the specified item.
  • GetList – Returns a schema for the specified list.
  • GetListAndView – Returns the list and view schemas for the specified list.
  • GetListCollection – Returns the names and GUIDs for all the lists in the site.
  • GetListItemChanges – Returns changes made to the list since the specified date and time.
  • GetListItems – Returns information about items in the list based on the specified query.
  • UpdateList – Updates a list based on the specified field definitions and list properties.
  • UpdateListItems – Adds, deletes, or updates the specified items in a list on the current site.

 

Add ListItem and attachment To list through webservice

Here i am giving sample example  where end user add calls[issues] in call register[SharePoint List] in Asp.Net Web site.

Following is the screen which can be used to insert a call (queries, issues) into call register.

Fig 1: Landing Page

Add ListItem

When user click on add call following screen is shown. With the help of screen is shown user can add call in call register .

Fig 2:Add List items.

On button submit click value stored in one wrapper class  and Communicate with SharePoint webservice as follow

Fig 3: Value stored in to the class object

And Now Pass this class object to Interface[wrapper class] in which using the SharePoint List WebService to insert an item into a list.

namespace CallRegisterInterface
{
    public class CallRegisterWebservice
    {
        //THIS IS GENERIC FUNCTION USED TO ADD THE ITEM TO LIST.
        public void AddNewCalls(clsCallRegister objCallRegister)
        {
            try
            {
                CallRegisterInterface.WSSCallRegister.Lists wsList = new CallRegisterInterface.WSSCallRegister.Lists();
                string listName = ConfigurationSettings.AppSettings[“WssCallRegisterList”];
                string wssUserName = ConfigurationSettings.AppSettings[“WssUserName”];
                string wssPassword = ConfigurationSettings.AppSettings[“WssPassword”];
                string wssDomain = ConfigurationSettings.AppSettings[“WssDomainName”];
                wsList.Credentials = new System.Net.NetworkCredential(wssUserName, wssPassword, wssDomain);               
                string fileName = objCallRegister.Content;            
                if (fileName !=string.Empty || fileName !=””)
                {
                    fileName = fileName.Substring(fileName.LastIndexOf(“\\”) + 1);                  
                }       
                //Create the batch file to add new item to list.
                string strBatch = “<Method ID=’1′ Cmd=‘New’>” +
                   “<Field Name=’Title’>” + objCallRegister.DealerCode + “</Field>” +
                   “<Field Name=’Issuing_x0020_Date’>” + objCallRegister.IssuingDate + “</Field>” +
                   “<Field Name=’Dealer_x0020_Name’>” + objCallRegister.DealerName + “</Field>” +
                   “<Field Name=’UserID’>” + objCallRegister.UserId + “</Field>” +
                   “<Field Name=’Module’>” + objCallRegister.ModuleName + “</Field>” +
                   “<Field Name=’Program_x0020_Name’>” + objCallRegister.ProgramName + “</Field>” +
                   “<Field Name=’Brief_x0020_Error’>” + objCallRegister.BriefError + “</Field>” +
                   “<Field Name=’Problem_x0020_Description’>” + objCallRegister.ProblemDescription + “</Field>” +
                   “<Field Name=’Role_x0020_Name’>” + objCallRegister.RoleName + “</Field>” +
                   “<Field Name=’Status’>New</Field>” +
                   “<Field Name=’Call_x0020_Priority’>”+objCallRegister.Priority+”</Field>” +
                   “<Field Name=’Reporting_x0020_Person_x0020_Nam’>” + objCallRegister.ReportingToName + “</Field>” +
                   “<Field Name=’Reporting_x0020_Person_x0020_Ema’>” + objCallRegister.ReportingToEmailID + “</Field>” +
                   “<Field Name=’Estimated_x0020_Completion_x0020′>” + null + “</Field>” +
                   “<Field Name=’Solution_x0020_Provided’>-</Field></Method>”;
               

                XmlDocument xmlDoc = new System.Xml.XmlDocument();
                System.Xml.XmlElement elBatch = xmlDoc.CreateElement(“Batch”);
                elBatch.SetAttribute(“OnError”, “Continue”);
                elBatch.SetAttribute(“ListVersion”, “1”);
                elBatch.InnerXml = strBatch;
                XmlNode ndReturn = wsList.UpdateListItems(listName, elBatch);
                if (ndReturn.ChildNodes.Item(0).InnerText != “0x00000000”)
                {  
                }
                else
                {
                    //Add attachment to the list
                    if (fileName != string.Empty || fileName != “”)
                    {
                        byte[] fileContents = objCallRegister.FileContent;                      
                        string return_value = wsList.AddAttachment(listName, ndReturn.SelectSingleNode(“//@ows_ID”).Value, fileName, fileContents);
                        wsList.Dispose();
                    }                  
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

My Calls

When user click on My Call following screen is shown. With the help of screen is shown user can view calls in call register.

  

         Fig 4:View ListItem in Gridview 

On Page Load event we call GetListData function to communicate with sharepoint web service GetListItems Webmethod  as follow and return dataset  and we bind dataset to gridview control

//THIS IS GENERIC FUNCTION USED TO RETRIEVE LIST ITEM.
       public DataSet GetListData()
       {

           CallRegisterInterface.WSSCallRegister.Lists wsList = new CallRegisterInterface.WSSCallRegister.Lists();
           string strUserID = GlobalData.UserID.ToString();          
           string listName = ConfigurationSettings.AppSettings[“WssCallRegisterList”];
           string wssUserName = ConfigurationSettings.AppSettings[“WssUserName”];
           string wssPassword = ConfigurationSettings.AppSettings[“WssPassword”];
           string wssDomain = ConfigurationSettings.AppSettings[“WssDomainName”];
           wsList.Credentials = new System.Net.NetworkCredential(wssUserName, wssPassword, wssDomain);

           //xmldocument to create necessary nodes for getalllistitems method..
           XmlDocument xdoc = new XmlDocument();
           XmlNode queryNode = xdoc.CreateNode(XmlNodeType.Element, “Query”, “”);

           string strQuery = @”<Where><And><And><IsNotNull><FieldRef Name=’Title’ /></IsNotNull>” +
                             “<Neq><FieldRef Name=’Status’ /><Value Type=’Choice’>Closed</Value>” +
                             “</Neq></And><Eq><FieldRef Name=’UserID’ /><Value Type=’Text’>” + strUserID + “</Value></Eq></And></Where>”+
                              “<OrderBy><FieldRef Name=’ID’ Ascending=’False’ /></OrderBy>”;

           //get all list items whose id is greater than zero i.e all list item..           
           queryNode.InnerXml = strQuery;
           XmlNode viewNode = xdoc.CreateNode(XmlNodeType.Element, “ViewFields”, “”);
           string strViewFields = “”;//for all fields”<FieldRef Name=\”ID\” /><FieldRef Name=\”Title\” />”;
           viewNode.InnerXml = strViewFields;
           XmlNode nodeQueryOption = xdoc.CreateNode(XmlNodeType.Element, “QueryOptions”, “”);
           nodeQueryOption.InnerXml = “<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>”;
           string rowlimit = “100”;
           string webid = “”;

           //get items from test sharepoint list..
           XmlNode xmlnodeinput = wsList.GetListItems(listName, “”, queryNode, viewNode, rowlimit, nodeQueryOption, webid);
           wsList.Dispose();

           XmlDocument wdoc = new XmlDocument();
           wdoc.LoadXml(xmlnodeinput.InnerXml);
           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           using (XmlReader reader = new XmlNodeReader(wdoc.DocumentElement))
           {
               ds.ReadXml(reader);
               reader.Close();
           }
           return ds;
       }

Binding Dataset with Gridview

                                                                          

Fig5: Binding Gridview 

When user wants to edit existing added listItem following screen appears,

Fig

6:Update Existing ListItem through webservice.

Following sample code for How to update an Existing SharePoint list item using a CAML Query to the SharePoint Lists

//THIS IS GENERIC FUNCTION USED TO UPDATE THE ITEM TO LIST.
        public void UpdateItemtoList()
        {
            try
            {
                CallRegisterInterface.WSSCallRegister.Lists wsList = new CallRegisterInterface.WSSCallRegister.Lists();
                string strUserID = GlobalData.UserID.ToString();
                string listName = ConfigurationSettings.AppSettings[“WssCallRegisterList”];
                string wssUserName = ConfigurationSettings.AppSettings[“WssUserName”];
                string wssPassword = ConfigurationSettings.AppSettings[“WssPassword”];
                string wssDomain = ConfigurationSettings.AppSettings[“WssDomainName”];
                wsList.Credentials = new System.Net.NetworkCredential(wssUserName, wssPassword, wssDomain);

                string fileName = clsCallRegister.UpdateContent;               
                if (fileName != string.Empty || fileName != “”)
                {
                    fileName = fileName.Substring(fileName.LastIndexOf(“\\”) + 1);                 
                }                  

                //Create the batch file to update item to list.
                string strBatch = “<Method ID=’1′ Cmd=‘Update’>” +
                   “<Field Name=’ID’>” + clsCallRegister.UpdateItemID + “</Field>” +
                   “<Field Name=’Module’>” + clsCallRegister.UpdateModuleName + “</Field>” +
                    “<Field Name=’Issuing_x0020_Date’>” + clsCallRegister.UpdateIssuingDate+ “</Field>” +
                   “<Field Name=’Program_x0020_Name’>” + clsCallRegister.UpdateProgramName + “</Field>” +
                   “<Field Name=’Brief_x0020_Error’>” + clsCallRegister.UpdateBriefError + “</Field>” +
                   “<Field Name=’Problem_x0020_Description’>” + clsCallRegister.UpdateProblemDescription + “</Field>” +
                   “<Field Name=’Role_x0020_Name’>” + clsCallRegister.UpdateRoleName + “</Field>” +
                   “<Field Name=’Status’>”+clsCallRegister.UpdateStatus+”</Field>” +                 
                   “<Field Name=’Reporting_x0020_Person_x0020_Nam’>” + clsCallRegister.UpdateReportingToName + “</Field>” +
                   “<Field Name=’Reporting_x0020_Person_x0020_Ema’>” + clsCallRegister.UpdateReportingToEmailID + “</Field></Method>”;

                XmlDocument xmlDoc = new System.Xml.XmlDocument();
                System.Xml.XmlElement elBatch = xmlDoc.CreateElement(“Batch”);
                elBatch.SetAttribute(“OnError”, “Continue”);
                elBatch.SetAttribute(“ListVersion”, “1”);
                elBatch.InnerXml = strBatch;
                XmlNode ndReturn = wsList.UpdateListItems(listName, elBatch);              
                if (ndReturn.ChildNodes.Item(0).InnerText != “0x00000000”)
                {
                }
                else
                {
                    //Delete Existing Attachment
                    if (fileName != string.Empty || fileName != “”)
                    {
                        if (clsCallRegister.UpdateDeleteAttachment)
                        {
                            XmlNode ndAttach = wsList.GetAttachmentCollection(listName, ndReturn.SelectSingleNode(“//@ows_ID”).Value);
                            XmlNodeList ndsAttach = ndAttach.ChildNodes;
                            for (int i = 0; i < ndsAttach.Count; i++)
                            {
                                string delUrl = ndsAttach[i].InnerText;
                                wsList.DeleteAttachment(listName, ndReturn.SelectSingleNode(“//@ows_ID”).Value, delUrl);
                            }                          
                        }
                        //Uploading New Attachment
                        byte[] fileContents = clsCallRegister.UpdateFileContent;                     
                        string return_value = wsList.AddAttachment(listName, ndReturn.SelectSingleNode(“//@ows_ID”).Value, fileName, fileContents);
                        wsList.Dispose();                      
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Happy Coding !!

This entry was posted in Sharepoint. Bookmark the permalink.

Leave a comment