Insert SharePoint List Item through Web Service

Util.GetLookupStringValue("DEFECTMGNTLIST", "[Category]")  –> Get from table

private bool AddListItem(string appName,
     string appVersion,
     string appProject,
     string appPhase,
     string appTitle,
     string severity,
     string impact,
     string raisedBy,
     DateTime raisedDate,
     string refNo, string status, string comment, string recType)
{
         string strBatch = "<Method ID=’1′ Cmd=’New’>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "AppName") + "’>" + appName + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "AppVersion") + "’>" + appVersion + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Project") + "’>" + appProject + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Phase") + "’>" + appPhase + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Title") + "’>" + appTitle + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Severity") + "’>" + severity + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Impact") + "’>" + impact + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "RaisedBy") + "’>" + raisedBy + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "ReferenceNo") + "’>" + refNo + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Status") + "’>" + status + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "Comments") + "’>" + comment + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "RecType") + "’>" + recType + "</Field>" +
             "<Field Name=’" + Util.GetLookupStringValue("DEFECTMGNTLIST", "RaisedDate") + "’>" + raisedDate.ToString("yyyy-MM-dd hh:mm:ss") + "</Field></Method>";

MyPortalSharePointITList.Lists listSvc = new MyPortalSharePointITList.Lists();
listSvc.Url = Util.GetLookupStringValue("DEFECTMGNTLIST", "URL");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential(“User”, “Password”, "Domain");
System.Net.CredentialCache cache = new System.Net.CredentialCache();
cache.Add(new Uri(“http://servername/sites/abc”), “NTLM”, cred);
listSvc.Credentials = cache;

         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 = listSvc.UpdateListItems(Util.GetLookupStringValue("DEFECTMGNTLIST", "ListID"), elBatch);
         if (ndReturn.InnerXml.IndexOf("<ErrorCode>0x00000000</ErrorCode>") < 0)
             return false;
         else
             return true;

     }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s