Wednesday, January 4, 2012

Add multiple items to SharePoint List by using batch command

We'd known how to add one new item to the SharePoint List by using Items.Add() method. But adding multiple items is really more complex. In this post, I will give you a solution for it by using batch command.
I keep using the list named Employee with 5 columns: Title, Birthday, Male, Position, Salary. In this scenario, I have an xml file named Employees.xml with 2 items and I want to add them all to the Employee list

Employees.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
      <Employee>
            <Title>Le Dung</Title>
            <Birthday>1990/2/2</Birthday>
        <Male>Yes</Male>
            <Position>Developer</Position>
            <Salary>800</Salary>
      </Employee>
      <Employee>
            <Title>Phan Tu</Title>
            <Birthday>1990/1/1</Birthday>
        <Male>Yes</Male>
            <Position>Developer</Position>
            <Salary>800</Salary>
      </Employee>     
</Employees>

The following is the complete code to adding multiple items from xml file to Sharepoint list using batch command.

        protected void addMultiItems()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPWeb web = SPContext.Current.Web;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("C:\\Employees.xml");
                XmlElement elmRoot = xmlDoc.DocumentElement;
                XmlNodeList elemList = elmRoot.GetElementsByTagName("Employee");
                if (elemList.Count > 0)
                {                                       
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["Employee"];
                    lock (this)
                    {
                        StringBuilder addXml = new System.Text.StringBuilder(51200);
                        addXml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError='Continue'>");
                        for(int i=0; i<elemList.Count; i++)
                        {                           
                            addXml.Append("<Method>");
                            addXml.AppendFormat("<SetList Scope=\"Request\">{0}</SetList>", list.ID);
                            addXml.Append("<SetVar Name=\"ID\">New</SetVar>");
                            addXml.Append("<SetVar Name=\"Cmd\">Save</SetVar>");
                            addXml.AppendFormat("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{0}</SetVar>", elemList[i]["Title"].InnerText != null ? elemList[i]["Title"].InnerText.ToString() : "");
                            string date = elemList[i]["Birthday"].InnerText != null ? elemList[i]["Birthday"].InnerText.ToString() : "";
                            if (date != string.Empty)
                            {
                                date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Parse(date));
                                addXml.AppendFormat("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Birthday\">{0}</SetVar>", date);
                            }                           
                            addXml.AppendFormat("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Position\">{0}</SetVar>", elemList[i]["Position"].InnerText != null ? elemList[i]["Position"].InnerText.ToString() : "");
                            addXml.AppendFormat("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Salary\">{0}</SetVar>", elemList[i]["Salary"].InnerText != null ? elemList[i]["Salary"].InnerText.ToString() : "");

                            string strMale = elemList[i]["Male"].InnerText != null ? elemList[i]["Male"].InnerText.ToString() : "";
                            if (strMale != string.Empty)
                            {
                                if (strMale.ToUpper() != "YES")
                                {
                                    addXml.AppendFormat("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Male\">{0}</SetVar>", "False");
                                }
                                else
                                {
                                    addXml.AppendFormat("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Male\">{0}</SetVar>", "True");
                                }
                            }
                            addXml.Append("</Method>");
                        }                       
                        addXml.Append("</ows:Batch>");                       
                        web.ProcessBatchData(addXml.ToString());                       
                    }
                    web.AllowUnsafeUpdates = false;                                                   
                }
            });
        }

Hope this helps!

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews