Another question I got today was how you could use the CAML Query Builder dls to add a new item to a SharePoint list.
Indeed, the CAML Query Builder tool consists of a user interface and several dlls:
- U2U.SharePoint.CAML.dll: this dll contains a class named Builder that you can use to build the CAML queries you need
- U2U.SharePoint.CAML.Server.dll: this dll contains a class named Helper that you can use to execute CAML queries through the SharePoint object model
- U2U.SharePoint.CAML.Client.dll: this dll contains a class named Helper that you can use to execute CAML queries through the SharePoint web services
I think it is not commonly known that you can use the dlls without the user interface and that you can use them within your SharePoint projects.
In this post I will explain how you can use the U2U.SharePoint.CAML.Client dll to add a new list item to a SharePoint list, which means throught the SharePoint web services.
Lets say I have a U2U Contact list and I want to add a new colleague. This list contains the following columns: Last Name, First Name, Company, Business Phone and Email Address.
If you want to add a list item via the SharePoint web services, you need to execute the UpdateListItem method of the Lists web service. This method requires a CAML query that has the following syntax:
So the first step is to build your CAML query. Instantiate the U2U.SharePoint.CAML.Builder class and pass it the type of method that you want to build:
U2U.SharePoint.CAML.Builder camlBuilder = new U2U.SharePoint.CAML.Builder(U2U.SharePoint.CAML.Enumerations.CamlTypes.UpdateListItems);
This will create an xml document with the root node required by the update method. In this case it is <Batch>.
Then build you caml query with calls to the following methods:
camlBuilder.UpdateBatchNode(Caml.Constants.Batch.OnError, "Continue");
camlBuilder.AddMethodNode(1, "New");
camlBuilder.AddFieldNode(1, "Title", "DeRudder");
camlBuilder.AddFieldNode(1, "FirstName", "Kevin");
camlBuilder.AddFieldNode(1, "Email", "kevin@u2u.be");
Now you can execute the UpdateListItem metod on the U2U.SharePoint.CAML.Client dll:
U2U.SharePoint.CAML.Client.Helper helper = new U2U.SharePoint.CAML.Client.Helper("http://wss.u2ucourse.com", "U2U Contacts", camlBuilder.CamlDocument);
By using this constructor of the helper class you pass SharePoint URL, list name and CAML query document in one time. So calling the following method is sufficient to get your query correctly executed:
XmlNode result = helper.UpdateListItems();
This method calls the UpdateListItem method on the Lists web service and passes the CAML to it. The returned value is an xml node that contains a success message or an error message. If the xml contains the following <ErrorCode>0x00000000</ErrorCode>, it means your query executed successfully and you item is added to the list. If the ErrorCode element contains something else, inspect the xml string closely because the error message will be contained in it.
Please, let me know if you have question on other methods and I will post an explanation as soon as possible.