Wednesday, October 31, 2012

How to manage and query items in folders of SharePoint Lists pro-grammatically in C#.

Here I am going to provide you very simple way of managing your SharePoint List Items in Folders. In this way we will first check that folder with this name exists or not. If it exists then it will insert item in that folder and if not then it will create a new folder of this name and put item inside this newly created folder. 

At first we need to include this method in our project to check that particular folder exists or not :

 public static bool FolderExists(string url, SPWeb web)
        {
            try
            {
                if (web.GetFolder(url).Exists)
                {
                    return true;
                }
                return false;
            }
            catch (ArgumentException ex)
            {
                Logger.Write(ex);
                return false;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }


This methods will check that a folder with the supplied parameters exists or not. And then add item as per values given in corresponding folder OR if does not exists will create a new one  and then add item as per values given in corresponding folder.  

public void UpdateDemoFolderInListA(SPWeb objSPWeb, string strDemo)
        {
            if (objSPWeb != null)
            {
                SPList objSPListListA = objSPWeb.Lists.TryGetList("ListA");
                if (objSPListSavedSearch != null)
                {
                    SPFolder spFolder;
                    if (FolderExists(objSPWeb.ServerRelativeUrl + "/Lists/ListA/" + strDemo, spWeb))
                    {
                        spFolder = spWeb.GetFolder(objSPWeb.ServerRelativeUrl + "/Lists/ListA/" + strDemo);
                    }
                    else
                    {
                        SPListItem newFolder = objSPListSavedSearch.Items.Add(spWeb.ServerRelativeUrl + "/" + objSPListListA.RootFolder.Url, SPFileSystemObjectType.Folder, strDemo);
                        newFolder.Update();
                        spFolder = spWeb.GetFolder(spWeb.ServerRelativeUrl + "/Lists/ListA/" + strDemo);
                    }
                        SPListItem item = objSPListListA.Items.Add(spFolder.ServerRelativeUrl, SPFileSystemObjectType.File);
                        item["Column1"] = Value1;
                        item["Column2"] = Value2;
                        item["Column3"] = Value3;
                        item.Update();
                    }
                }
            }
        }


You can use this approach to extend functionality such as to add item in some restriction such as maximum no. of items in the folder or date wise folder or user logged in folder items update.Anyways querying for item inside a particular folder with defined URL will a performance boost.  Hope it will help.

No comments:

Post a Comment