Thursday 19 March 2009

CAML (Collaborative Application Markup Language) PART I


CAML (Collaborative Application Markup Language) is an XML based markup language used with the family of Microsoft SharePoint technologies (Windows Sharepoint Services and Office SharePoint Server). Unlike plain XML, CAML contains specific groups of tags to both define and display (render) data.

This is base definition for CAML, but CAML allows you to query Sharepoint at any single level, we can say that CAML is the T-SQL of Sharepoint. In fact the notation is quite similar in some ways, or at least we can realize for the code that CAML is not a program language, it is a propel query language based in XML.

The best way of using CAML in your C#.NET/VB.NET code, or even in your Java code, is by calling the out-of-the-box web service that comes with Sharepoint. This web services can be called from anywhere but they have always to point your server, in few words, they can not be moved.

GetListItems(…) is the method you need in order to use CAML. Top do this the best way is do the following:

In your project Right click in “Web References” and “Add Web Reference”



If tou type the name of your server ie: called "mymoss", in order to use the query you will have: http://mymosss/_vti_bin/Lists.asmx?op=GetListItems



private WSSObject[] ListFilesFromHistory(string _sSiteUrl, string _sHistoryNameList, string _sRowLimit)
{
string _sows_Edited_x0020 ="";
int iCounter = 0;
WSSObject[] wFolders = null;
string siteUrl = _sSiteUrl;//this.UserWebSite;

MOSS2007.GetListCollection.Lists wsList = new MOSS2007.GetListCollection.Lists();
wsList.Credentials = System.Net.CredentialCache.DefaultCredentials;

wsList.Url = siteUrl + @"/_vti_bin/lists.asmx";
// get a list of all top level lists
XmlNode allLists = wsList.GetListCollection();

// load into an XML document so we can use XPath to query content
XmlDocument allListsDoc = new XmlDocument();

// Loading all the stuff in the XmlDocument class
allListsDoc.LoadXml(allLists.OuterXml);

XmlNamespaceManager ns = new XmlNamespaceManager(allListsDoc.NameTable);
ns.AddNamespace("d", allLists.NamespaceURI);

// now get the GUID of the document library we are looking for
//XmlNode dlNode = allListsDoc.SelectSingleNode("/d:Lists/d:List[@FeatureId='00bfea71-e717-4e80-aa17-d0c71b360101']", ns);

//It gets all the nodes related with that ID
//XmlNodeList dlNodeList =allListsDoc.SelectNodes("/d:Lists/d:List[@FeatureId='" + GetFeatureIDFromLibrary("Reporting Templates").ToString() + "']", ns);
XmlNodeList dlNodeList = null;

dlNodeList = allListsDoc.SelectNodes"/d:Lists/d:List[@Title='"+_sHistoryNameList+"']", ns);

//The WWSObject is initilized
wFolders = new WSSObject[dlNodeList.Count];

//The WWSObject is filled.
foreach (XmlNode dlNode in dlNodeList)
{
wFolders[iCounter] = new WSSObject (dlNode.Attributes["Title"].Value, dlNode.Attributes["Title"].Value);

// obtain the GUID for the document library and the webID
string documentLibraryGUID = dlNode.Attributes["ID"].Value;
string webId = dlNode.Attributes["WebId"].Value;

// Creating ViewFields CAML
XmlDocument viewFieldsDoc = new XmlDocument();
XmlNode ViewFields = AddXmlElement(viewFieldsDoc, "ViewFields", "");
AddFieldRef(ViewFields, "GUID");
AddFieldRef(ViewFields, "ContentType");
AddFieldRef(ViewFields, "BaseName");
AddFieldRef(ViewFields, "Modified");
AddFieldRef(ViewFields, "EncodedAbsUrl");
AddFieldRef(ViewFields, "CheckedOutDate");
AddFieldRef(ViewFields, "Subject");
AddFieldRef(ViewFields, "Title");
AddFieldRef(ViewFields, "Author");
AddFieldRef(ViewFields, "Date Modified");
AddFieldRef(ViewFields, "Format");
AddFieldRef(ViewFields, "Version");
AddFieldRef(ViewFields, "URL");
AddFieldRef(ViewFields, "Edited_x0020_by");

//######################################
//##ows_Edited_x0020_by="14;#Jon Taylor"
//######################################

// create QueryOptions CAML
XmlDocument queryOptionsDoc = new XmlDocument();
XmlNode QueryOptions = AddXmlElement(queryOptionsDoc, "QueryOptions", "");
//XmlNode Query = CreateNode("Query", @" " + _sows_Edited_x0020);
XmlNode Query = CreateNode("Query", _sows_Edited_x0020);

AddXmlElement(QueryOptions, "Folder", wFolders[iCounter].Name);
//AddXmlElement(QueryOptions, "IncludeMandatoryColumns", "FALSE");


//// this element is the key to getting the full recusive list
XmlNode node = AddXmlElement(QueryOptions, "ViewAttributes", "");
AddXmlAttribute(node, "Scope", "Recursive");

// obtain the list of items in the document library
//XmlNode listContent = wsList.GetListItems(documentLibraryGUID, null, null, ViewFields, null, QueryOptions, webId);
//XmlNode listContent = wsList.GetListItems(documentLibraryGUID, null, null, ViewFields, null, QueryOptions, webId);
XmlNode listContent = wsList.GetListItems(documentLibraryGUID, null, Query, ViewFields, _sRowLimit, QueryOptions, webId);

//The results are loaded
XmlDocument xmlResultsDoc = new XmlDocument();
xmlResultsDoc.LoadXml(listContent.OuterXml);


ns = new XmlNamespaceManager(xmlResultsDoc.NameTable);
ns.AddNamespace("z", "#RowsetSchema");

//I dunno what it does...
XmlNodeList rows = xmlResultsDoc.SelectNodes("//z:row", ns);

if (rows.Count > 0)
{
wFolders[iCounter].Objects = new WSSObject[rows.Count];
int iFileCounter = 0;
foreach (XmlNode row in rows)
{
string _sCheckBy = row.Attributes["ows_CheckoutUser"] != null ? row.Attributes["ows_CheckoutUser"].Value : "";
//##Every file is added
//##
//##If the file doesn't have extension we add a ""
//if (row.Attributes["ows_DocIcon"] == null) wFolders[iCounter].Objects[iFileCounter++] = new WSSObject(row.Attributes ["ows_FileLeafRef"].Value, row.Attributes["ows_Editor"].Value, row.Attributes["ows_EncodedAbsUrl"].Value, row.Attributes["ows_FileRef"].Value, row.Attributes["ows_Modified"].Value, row.Attributes["ows_ContentType"].Value, row.Attributes["ows_GUID"].Value, "", _sCheckBy);
//##Else we add the right extension
wFolders[iCounter].Objects[iFileCounter] = new WSSObject(row.Attributes ["ows_FileLeafRef"].Value, "", row.Attributes["ows_EncodedAbsUrl"].Value, row.Attributes["ows_FileRef"].Value, row.Attributes["ows_Modified"].Value, row.Attributes["ows_ContentType"].Value, row.Attributes["ows_GUID"].Value, "", _sCheckBy);
wFolders[iCounter].Objects [iFileCounter].HistoryAuthor = row.Attributes["ows_Author"].Value != null ? row.Attributes["ows_Author"].Value : "";
wFolders[iCounter].Objects [iFileCounter].HistoryFormat = row.Attributes["ows_FileLeafRef"].Value!=null?row.Attributes["ows_FileLeafRef"].Value:"";
wFolders[iCounter].Objects[iFileCounter].HistoryModified = row.Attributes["ows_Modified"].Value != null ? row.Attributes["ows_Modified"].Value : "";
wFolders[iCounter].Objects[iFileCounter].HistorySubject = row.Attributes["ows_Subject"].Value != null ? row.Attributes["ows_Subject"].Value : "";
wFolders[iCounter].Objects[iFileCounter].HistoryTitle = row.Attributes["ows_FileLeafRef"].Value != null ? row.Attributes["ows_FileLeafRef"].Value : "";
wFolders[iCounter].Objects[iFileCounter].HistoryURL = row.Attributes["ows_URL"].Value != null ? row.Attributes["ows_URL"].Value : "";
wFolders[iCounter].Objects[iFileCounter].HistoryVersion = row.Attributes["ows_FileLeafRef"].Value != null ? row.Attributes["ows_FileLeafRef"].Value : "";

iFileCounter++;
//##
//Console.WriteLine(row.Attributes["ows_ContentType"].Value + " " + row.Attributes ["ows_GUID"].Value + " :: " + row.Attributes["ows_BaseName"].Value);
}
}

iCounter++;
}

//CurrentFolderStructure = wFolders;

return wFolders;
}