Thursday 13 October 2011

Building a Document Management System with SharePoint 2010 - Part 11–Creating Content Types from the Client Model

This post is more about how to improve the performance of your DMS to the extreme using the client model:
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client.dll
Well, what I mean with best performance is to be able to not just create Content Types with the Client Model (I don’t cover this part here because I assume you already have the content types), it is the fact we can assign Content Types, something that is not allowed in the client model unless you use some tricks.

What I am going to do is to assign two different Content Types (Client Folder, Matter Folder) so we can have a client/matter structure in our DMS. Both Content Types are already created and inherits from the native folder Content Type. So we will have something like this:


+Client1----+Matter1
                   |
                   +Matter2
                   |
                   +Matter3

Before going ahead wit the chunk of code, I will explain you how it works.
1- You create your folder.
2- You get the id from your content type.
3- You apply the id into this field: “ContentTypeId”. I update the client number and client description.

ListItem item = items[0];
item["ContentTypeId"] = _ctClientFolder.Id;
item["client"] = _sClientNumber;
item["clientdescription"] = _sClientDescription;
item.Update();
clientContext.ExecuteQuery();



I am going to add the code below, to use it, be sure you add:
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client.dll
Into your references and :

using Microsoft.SharePoint.Client;
To call the method just try this:
CreateClientMatter(@"http://mysharepointserver", "Documents", "00000272", "my client", "00000018", "my matter");
Just copy and paste this code into your project, if the client/matter is not there it will created and add the properties:
        private List ListCheck(ClientContext clientContext, Web _wListWeb, string _sListName)
        {
            List _lExistingList = null;
            try
            {
                //###############################
                //## We check if the file exists
                //###############################
                _wListWeb = clientContext.Web;
                ListCollection _lCollectionOfLists = _wListWeb.Lists;
                IEnumerable<List> existingLists = clientContext.LoadQuery(
                        _lCollectionOfLists.Where(
                        list => list.Title == _sListName)
                        );
                clientContext.ExecuteQuery(); 
                _lExistingList = existingLists.FirstOrDefault();
                //############################### 
            }
            catch (Exception ex)
            {
                throw new ArgumentNullException();
            }
            return _lExistingList;
        }
        private void CreateClientFolder(string _sServerURL, ClientContext clientContext, Web _wListWeb, List _lList, string _sListName, string _sClientNumber, string _sClientDescription)
        {
            //## Variables.
            Folder _fExistingFolder = null;
            ContentType _ctClientFolder = null; 
            //## We get all the folders
            FolderCollection _afolders = _lList.RootFolder.Folders; 
            //## Building the URL (folder)
            String _sClientfolderUrl = String.Format("/{0}/{1}", _sListName, _sClientNumber);
            //## We check if the URL exists
            IEnumerable<Folder> _iClientExistingFolders = clientContext.LoadQuery<Folder>(
                _afolders.Where(
                folder => folder.ServerRelativeUrl == _sClientfolderUrl)
                );
            clientContext.ExecuteQuery();
            //## Getting the result
            _fExistingFolder = _iClientExistingFolders.FirstOrDefault();
            if (_fExistingFolder == null)                       
            {
                //## Adding the folder
                _wListWeb.Folders.Add(_sServerURL + @"/" + _sListName + @"/" + _sClientNumber);
                clientContext.ExecuteQuery();
                //## Quering our folder
                CamlQuery query = new CamlQuery();
                query.ViewXml = @"       <View>
                                             <Query>
                                                <Where>
                                                   <Eq>
                                                      <FieldRef Name='FileLeafRef'/>
                                                      <Value Type='Text'>"+_sClientNumber+@"</Value>
                                                   </Eq>
                                                </Where>
                                             </Query>
                                             <RowLimit>1</RowLimit>
                                          </View>"; 
                ListItemCollection items = _lList.GetItems(query);
                clientContext.Load(items);
                //## Getting the content types
                ContentTypeCollection _ctListOfContentTypes = clientContext.Web.AvailableContentTypes;
                clientContext.Load(_ctListOfContentTypes);
                clientContext.ExecuteQuery();
                //## Getting the Content Type Id
                foreach (ContentType _ctItem in _ctListOfContentTypes)
                {
                    if (_ctItem.Name == "Client Folder")
                    {
                        _ctClientFolder = _ctItem;
                        break;
                    }
                }
                //## This is the tricky part, where we convert our folder
                //## Into a content type. We add some metadata as well               
                if (items.Count > 0)
                {
                    ListItem item = items[0];
                    item["ContentTypeId"] = _ctClientFolder.Id;
                    item["client"] = _sClientNumber;
                    item["clientdescription"] = _sClientDescription;
                    item.Update();
                    clientContext.ExecuteQuery();
                }
            }
        }
 
        private void CreateMatterFolder(string _sServerURL, ClientContext clientContext, Web _wListWeb, List _lList, string _sListName,string _sClientNumber, string _sMatterNumber,string _sMatterDescription)
        {
            Folder _fExistingFolder = null;
            ContentType _ctMatterFolder = null; 
            FolderCollection _afolders = _lList.RootFolder.Folders; 
            String _sMatterfolderUrl = String.Format("/{0}/{1}/{2}", _sListName, _sClientNumber, _sMatterNumber); 
            IEnumerable<Folder> _iMatterExistingFolders = clientContext.LoadQuery<Folder>(
                _afolders.Where(
                folder => folder.ServerRelativeUrl == _sMatterfolderUrl)
                );
            clientContext.ExecuteQuery();
            _fExistingFolder = _iMatterExistingFolders.FirstOrDefault(); 
            if (_fExistingFolder == null)
            {
                _wListWeb.Folders.Add(_sServerURL + @"/" + _sListName + @"/" + _sClientNumber + @"/" + _sMatterNumber);
                clientContext.ExecuteQuery();
                //## Quering our folder
                CamlQuery query = new CamlQuery();
                query.ViewXml = @"       <View Scope='RecursiveAll'>
                                             <Query>
                                                <Where>
                                                   <Eq>
                                                      <FieldRef Name='FileLeafRef'/>
                                                      <Value Type='Text'>" + _sMatterNumber + @"</Value>
                                                   </Eq>
                                                </Where>
                                             </Query>
                                             <RowLimit>1</RowLimit>
                                          </View>";
 
                ListItemCollection items = _lList.GetItems(query);
                clientContext.Load(items); 
                //## Getting the content types
                ContentTypeCollection _ctListOfContentTypes = clientContext.Web.AvailableContentTypes;
                clientContext.Load(_ctListOfContentTypes);
                clientContext.ExecuteQuery(); 
                //## Getting the Content Type Id
                foreach (ContentType _ctItem in _ctListOfContentTypes)
                {
                    if (_ctItem.Name == "Matter Folder")
                    {
                        _ctMatterFolder = _ctItem;
                        break;
                    }
                }
                //## This is the tricky part, where we convert our folder
                //## Into a content type. We add some metadata as well               
                if (items.Count > 0)
                {
                    //## This is just in case the client number
                    //## has the same name than the new matter
                    ListItem item = items.Count == 2 ? items[1] : items[0];
                    item["ContentTypeId"] = _ctMatterFolder.Id;
                    item["matter"] = _sMatterNumber;
                    item["matterdescription"] = _sMatterDescription;
                    item.Update();
                    clientContext.ExecuteQuery();
                }
            }
        }
 
        public bool CreateClientMatter(string _sServerURL, string _sListName, string _sClientNumber, string _sClientName, string _sMatterNumber, string _sMatterName)
        {
            bool _bResult = false;
            List _lList = null;           
            try
            {
                using (var clientContext = new ClientContext(_sServerURL))
                {
                    Web _wListWeb = clientContext.Web;
                    //###############################
                    //## We check if the list exists
                    //###############################                   
                    _lList = ListCheck(clientContext, _wListWeb, _sListName);
                    //###############################                  
                    if (_lList != null)
                    {                       
                        //#################################
                        //## CREATE FOLDER CONTENT TYPES
                        //## IF THEY DON'T EXIST
                        //##################################
                        CreateClientFolder(_sServerURL, clientContext, _wListWeb, _lList, _sListName, _sClientNumber, _sClientName);
                        CreateMatterFolder(_sServerURL, clientContext, _wListWeb, _lList, _sListName, _sClientNumber,_sMatterNumber,_sMatterName);                    
                        //##################################
                        _bResult = true;
                    }
                }
            }
            catch (Exception ex)
            {
                string _sError = ex.ToString();               
            }
            return _bResult;
        }


Conclusion: In order to create a decent DMS be always sure you keep your server clean of web services and workflows, anything it can be done in the client side, it will leave the server free to do what it has to do, SERVE!

No comments: