Monday, December 27, 2010

ProcessBatchData in Sharepoint

Sometimes we need to update more than one item in a list, so looping through all items in sharepoint list would not be good practice, as it degrades performance.  


The better way to do this, is through ProcessBatchData. In this we construct a CAML string  contains a batch of commands to perform the updates. Then we should execute the commands by using the ProcessBatchData command of an SPWeb object.


Each command includes a Method element with SetVar subelements. 
The SetVar elements define 

  • Type of command e.g. <SetVar Name=\"Cmd\">
  • ID of the item e.g. <SetVar Name=\"ID\">
  • Fields to update. The fields are defined in the format <schema>#<internal_field_name> e.g. <SetVar Name=\"urn:schemas-microsoft-com:office:office#PersonID\">

The Guid of the List must be specified in <SetList> tag.


After all the commands are created, they are placed in an ows:Batch element. This element contains an OnError attribute that defines what happens if an exception occurs while the commands are executed


Thats it, here is the snippet 


Batch command to add a new item to Sharepoint list

As we are going to add a new item, just specify "New" in place of ID <SetVar Name \"ID\">. I have highlighted in the below code snippet.

 using (SPSite site = new SPSite("http://sitecollectionURL"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                        SPList list = web.Lists["ListName"];
                        string listGuid = list.ID.ToString();
                        string _xmlData = "";
                   
                        SPUser theUser = web.SiteUsers[_userID];
                        SPListItem item = list.Items.Add();
                        string _personID = theUser.ID.ToString() + ";#" + theUser.Name;

                        batch = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                            "<ows:Batch OnError=\"Return\">{0}</ows:Batch>" + "<Method ID=\"{0}\">" +
                            "<SetList>" + listGuid + "</SetList>" +
                            "<SetVar Name=\"Cmd\">Save</SetVar>" +
                            "<SetVar Name=\"ID\">New</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#PersonID\">" + _personID + "</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#XmlData\">" + _xmlData + "</SetVar>" +
                            "</Method>";
                    
                    string batchReturn = web.ProcessBatchData(batch);
                }
            }




Batch command to update an item in the Sharepoint list

To update an item in the sharepoint list, we just need to put the item id to update in the tag <SetVar Name \"ID\">. I have highlighted in the below code snippet.


        using (SPSite site = new SPSite("http://sitecollectionURL"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                        SPList list = web.Lists["ListName"];
                        string listGuid = list.ID.ToString();
                        string _xmlData = "";
                   
                        SPUser theUser = web.SiteUsers[_userID];
                        SPListItem item = list.Items.Add();
                        string _personID = theUser.ID.ToString() + ";#" + theUser.Name;

                        batch = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                            "<ows:Batch OnError=\"Return\">{0}</ows:Batch>" + "<Method ID=\"{0}\">" +
                            "<SetList>" + listGuid + "</SetList>" +
                            "<SetVar Name=\"Cmd\">Save</SetVar>" +
                            "<SetVar Name=\"ID\">ItemID</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#PersonID\">" + _personID + "</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#XmlData\">" + _xmlData + "</SetVar>" +
                            "</Method>";
                    
                    string batchReturn = web.ProcessBatchData(batch);
                }
            }

      

Thursday, December 9, 2010

JQuery - What? Why? How?

What is JQuery?
JQuery is a lightweight cross-browser JavaScript library. JQuery is designed to change the way that you write Javascript.


Why JQuery?
You can use simple JavaScript to perform all the functions that jQuery provides. Then why JQuery? It easy to create nice web effects in just a few lines of code. We may require 10-11 lines of code in Javascript to perform certain function, the same thing can be achieved through JQuery with just 1-2 lines of code (as it says Write less, Do More). And its very easy to learn too.


How to use it?
You can download the source code, from the jQuery website. There will two versions "Minified" and "Uncompressed".


Minified Vs Uncompressed
The minified version is best for the production environment. It will save you some bandwidth and some loading time. It is hard to debug.
The uncompressed version is for development, you can debug it.


How to use jQuery library in our ASP.Net project?
Once you have downloaded the JQuery and include the reference to the jQuery library file in your ASPX page.
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
        <script language="javascript">
            $(document).ready(function() {
            alert('test');
            });
        </script>


Launching Code on Document Ready
jQuery has a simple statement that checks the document and waits until the DOM is fully loaded.
Next, what is this $.. We can access the JQuery object using $. We can even use "jQuery" instead of $
eg: jQuery(document).ready(function() {
            alert('test');
            });

Hope this post will give you a brief idea about JQuery. You can learn more JQuery syntax here.