Wednesday, June 9, 2010

Using LinQ to query xml

-          Create a xml file with following structure.
<?xml version="1.0" encoding="utf-8" ?>

<Errors>

  <Error voucherId="1" message="Issues while saving data"/>

  <Error voucherId="1" message="Transaction Blocked"/>

  <Error voucherId="1" message="Account is Closed"/>

  <Error voucherId="3" message="This voucher has minumum balance."/>

  <Error voucherId="3" message="Some errors occured while saving the voucher."/>

</Errors>



-          Make sure your code file has following references
using System.Linq;

using System.Xml.Linq;


-          Open Visual Studio 2008 and create a new console application.  Write following code on the program.cs file.
        /// <summary>

        /// Class to represent an error message.  You can have similar class to represent your data

        /// </summary>

        public class Error

        {

            public string VoucherId { get; set; }

            public string ErrorMessage { get; set; }

        }



        static void Main(string[] args)

        {

            //Load XML document on to XDocument object

            XDocument xdoc = XDocument.Load("../../ErrorMessages.xml");



            //Query the XML document and read all the "Error" element.  Prepare a list of Error class from the returned data

            List<Error> errorCollection = (from errors in xdoc.Descendants("Error")

                                           where errors.Attribute("voucherId").Value=="1"

                                          select new Error

                                          {

                                              VoucherId = errors.Attribute("voucherId").Value,

                                              ErrorMessage = errors.Attribute("message").Value

                                          }

                                          ).ToList<Error>();



            //Loop through the returned collection and output the result.

            foreach (Error e in errorCollection)

            {

                Console.WriteLine("Voucher Id: " + e.VoucherId);

                Console.WriteLine("Error Message: " + e.ErrorMessage);

            }

            Console.ReadKey();

        }

    }

-         Execute this console application and it will print the selected nodes from xml


Regards,
Sudhir Kesharwani
Mob: +91 98225 11209

Updating URL field using Lists.asmx

Updating URL field using SharePoint Lists Web Service (Lists.asmx)



One thing that I always complain about SharePoint 2007 web service is lack of documentation and samples.  This is the most neglected areas of SharePoint 2007.  I hope this have been improved in SharePoint 2010 especially since Microsoft has added a lot of new web services.

I had been working on a jQuery based SharePoint solution to display a modal popup to all the visitors. Apart from displaying the jQuery pop up dialogue on page load, I also had to record the history about visitor.

I had to update the list items using Lists.asmx UpdateListItems() method. 

My custom list had following fields:-

Field Name
Type
Description
Title
Text
This field is used to record the login name of logged in user
URL
URL
This field is used to store the link of a text file hosted on server.

After struggling for some time I found that updating the URL field requires some additional attention, since the SharePoint URL field value consists of two parts <URL>, <Description>. Even when working with Object Modal we have to use SPUrlFieldValue.

Please make sure to follow these guidelines while updating the URL field through web services.

-          Please notice that URL needs to include “http://” e.g. http://www.google.com/, generally we write http://www.google.com/ and think that SharePoint should be intelligent enough to append http, but this is not the case.

-          The other thing is that we need to make sure to include a space between the comma (,) and description.  So our URL field value needs to have <URL including http://>,<SPACE><Description>, e.g. http://www.google.com/, Google.

Please pay extra attention while updating/Reading the URL field (through Web Service or thorough object modal).

Here is how the element should look in your soap envelope.

<Field Name=”URL”>http:\\www.google.com, Google</Field>


Here is my soap envelop for calling the UpdateListItems() method. I have omitted the other parts of calling the web service for clarity. (You get lots of examples through Google)
                // The SOAP Envelope
var soapEnv =
"<?xml version=\"1.0\" encoding=\"utf-8\"?> \
                <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
                xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
                xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
                <soap:Body> \
                <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \
                <listName>Audit</listName> \
 <updates> \
<Batch OnError=\"Continue\"> \

                                                                                <Method ID=\"1\" Cmd=\"New\"> \

                                                                                                <Field Name=\"Title\">Some Text</Field> \

<Field Name=\"URL\">http://www.google.com, Google</Field> \

                                                                                </Method> \

</Batch>

                </updates> \
                </UpdateListItems> \
                  </soap:Body> \
                </soap:Envelope>";


Hope this helps someone in need J

Regards,
Sudhir Kesharwani 
MCTS - MOSS 2007 | MCTS - WSS 3.0 | MCPD-EA | MCSD.NET