Search This Blog

Tuesday, 29 May 2012

SPList view creating/updating/deleting and displaying list view fields


In this post I have discussed SPList view create/edit/delete and get the list view fields using SharePoint object model.

Refer the below code snippet
using (SPSite osite = new SPSite("http://chesgh201tw1v:1605"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPList olist = oweb.Lists.TryGetList("Job Aid Documents");
                    if(olist != null)
                    {
                        #region Create View
                        string spViewQuery = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
                        System.Collections.Specialized.StringCollection spViewFields = new System.Collections.Specialized.StringCollection();    //Create string collection object
                        spViewFields.Add("ID");    //Add columns
                        spViewFields.Add("JobAidNo");
                        spViewFields.Add("LinkTitle");
                        spViewFields.Add("DocumentStatus");
                        spViewFields.Add("DocID");
                        string spViewName = "Sample View";        //Set view name
                        olist.Views.Add(spViewName,spViewFields,spViewQuery,30,true,false);
                        olist.Update();
                        Console.WriteLine("View created successfully");
                        #endregion 

                        #region Update View
                        SPView oView = olist.Views["Sample View"];
                        if (oView != null)
                        {
                            oView.ViewFields.Add("State");
                            oView.Update();
                        }
                        Console.WriteLine("View updated successfully");
                        #endregion

                        #region Display View
                        SPViewFieldCollection oViewFldColl = oView.ViewFields;
                        Console.WriteLine("View Name :: " + oView.Title);
                        Console.WriteLine("Fields Count :: " + oViewFldColl.Count);
                        foreach (string oFld in oViewFldColl)
                        {
                            Console.WriteLine("Field Name :: " + oFld);
                        }
                        #endregion 

                        #region Delete View
                        Console.Write("Do you want to delete view :: " + oView.Title);
                        Console.WriteLine();
                        string isDelete =Console.ReadLine();
                        if (isDelete == "y" || isDelete == "Y")
                        {
                            olist.Views.Delete(oView.ID);
                            olist.Update();
                        }
                        Console.WriteLine("View deleted successfully");

                        #endregion
                    }
                }
            }

No comments:

Post a Comment