serving the solutions day and night

Pages

Thursday, November 29, 2012

SharePoint 2010 - Create List, Update Document, Link to Other List using c#

This post contains mostly c# code. Code contains to create/update List, folder, link to other list and update word document.

using Microsoft.SharePoint.Client;

namespace CreateReportForPlacesV2
{
    public class SharePoint
    {
        // Defines a private SharePoint site, like "http://<Server Name>/"
        private string spSite = System.Configuration.ConfigurationManager.AppSettings["SPSite"].ToString();

        // Defines a private SharePoint document, like "Reporting Documents".
        private string spDoc = System.Configuration.ConfigurationManager.AppSettings["SPReportingDocument"].ToString();

        // Defines a private SharePoint place list name, like "Place Details".
        private string spPList = System.Configuration.ConfigurationManager.AppSettings["SPPlaceDetailsList"].ToString();

        // Defines a private SharePoint C list is used to link to Place list details.
        private string spCList = System.Configuration.ConfigurationManager.AppSettings["SPCList"].ToString();

// Create a cdate folder and a place id sub folder
// Upload a Word document from local folder to SharePoint.
// <param name="cDate">The cdate of the place.</param>
// <param name="pplid">The id of the place.</param>
// <param name="folder">The folder path of the document.</param>
// <param name="fileName">The file name of the document.</param>
        public void UpdateDocument(string cDate, string pplid, string folder, string fileName)
        {
            try
            {
                //connect SharePoint site
                ClientContext context = new ClientContext(this.spSite);
                Web web = context.Web;
                List docs = web.Lists.GetByTitle(this.spDoc);
                docs.EnableFolderCreation = true;

                //create a cdate folder
                Folder cDateFolder = docs.RootFolder.Folders.Add(cDate);
                cDateFolder.Update();

                //create a  place id sub folder
                Folder pplidFolder = cDateFolder.Folders.Add(pplid);
                pplidFolder.Update();

                context.ExecuteQuery();

                //get a word document from local folder
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(folder + "\\" + fileName);
                newFile.Url = fileName;
                newFile.Overwrite = true;

                //update a word document
                Microsoft.SharePoint.Client.File uploadFile = pplidFolder.Files.Add(newFile);
                context.Load(uploadFile);
                context.ExecuteQuery();
            }
            catch (Exception ex)
            {
                Log.UpdateLog("SharePoint.cs", "UpdateDocument", ex.ToString());
            }
        }

// Verify if the document list is exists or not.
// If not exists, create a document list into the SharePoint.
// List contains place  id, name, address, cdate and clink, word document link
// <param name="ppi">The object of the Place Info.</param>
        public void CreateList(PlaceInfo ppi)
        {
            //connect SharePoint site
            ClientContext context = new ClientContext(this.spSite);
            Web web = context.Web;
            List list = web.Lists.GetByTitle(this.spPList);

            ////Check the list exists or not
            CamlQuery query = new CamlQuery();
            query.ViewXml = @"<View>
            <Query>
                <Where>
                <And>
                    <Eq>
                        <FieldRef Name='CDate'/>
                        <Value Type='Text'>" + ppi.CDate + @"</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='PPLID'/>
                        <Value Type='Text'>" + ppi.PPLID + @"</Value>
                    </Eq>
                </And>
                </Where>
            </Query>
            </View>";

            ListItemCollection listItems = list.GetItems(query);
            context.Load(list);
            context.Load(listItems);
            context.ExecuteQuery();
            if (listItems.Count == 0)
            {
                //if not, create a new list
                ListItemCreationInformation newItem = new ListItemCreationInformation();
                ListItem listItem = list.AddItem(newItem);
                listItem["CDate"] = ppi.CDate;
                listItem["Title"] = ppi.Name;
                listItem["PPLID"] = ppi.PPLID;
                listItem["PlaceAddressLine1"] = ppi.AddressLine1;
                listItem["PlaceCity"] = ppi.City;
                listItem["PlaceState"] = ppi.State;
                listItem["PlaceZipCode"] = ppi.PostalCode;
                listItem["Crk"] = this.GetCID(ppi.FolderDate, ppi.HID);
                FieldUrlValue link = new FieldUrlValue();
                link.Url = this.spSite + "/" + this.spDoc + "/" + ppi.FolderDate + "/" + ppi.PPLID;
                link.Description = "View Audit Reports";
                listItem["DocumentsLink"] = link;
                listItem.Update();
                context.ExecuteQuery();
            }
        }
// Gets the Crk ID of the  place.
// <param name="cDate">The cdate of the  place.</param>
// <param name="hid">The hid number of the  place.</param>
// <returns>The crk id.</returns>
        private string GetCID(string cDate, string hid)
        {
            //connect SharePoint site
            ClientContext context = new ClientContext(this.spSite);
            Web web = context.Web;
            List list = web.Lists.GetByTitle(this.spCList);
          
            //find the crk id.
            CamlQuery query = new CamlQuery();
            query.ViewXml = @"<View>
                <Query>
                    <Where>
                    <And>
                        <Eq>
                            <FieldRef Name='CDate'/>
                            <Value Type='Text'>" + cDate + @"</Value>
                        </Eq>
                        <Eq>
                            <FieldRef Name='HID'/>
                            <Value Type='Text'>" + hid + @"</Value>
                        </Eq>
                    </And>
                    </Where>
                </Query>
                </View>";

            ListItemCollection listItems = list.GetItems(query);
            context.Load(list);
            context.Load(listItems);
            context.ExecuteQuery();
            string crkListItemId = string.Empty;
            foreach (ListItem item in listItems)
            {
                crkListItemId = item["ID"].ToString();
            }
            return crkListItemId;
        }
    }

No comments: