Wednesday, March 6, 2013

Add SharePoint Custom View Programatically




 SPWeb oWeb = SPContext.Current.Web;

            SPList oList = oWeb.Lists["YourList"];   
            oWeb.AllowUnsafeUpdates=true;

           SPViewCollection oViewCollection = oList.Views; 

           string strViewName = "Ravikumar-View";  

            System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();

            //The fields that you want on the view
            viewFields.Add("Name");

            viewFields.Add("Type");


            string query = "<Where><And><IsNotNull><FieldRef Name='Name' /></IsNotNull><Eq><FieldRef Name='EId' /><Value Type='Number'>288</Value></Eq></And></Where>";
                                                                       
            oViewCollection.Add(strViewName, viewFields, query, 7, true, false);


             oWeb.Update();
             oWeb.AllowUnsafeUpdates=false;

No comments:

Post a Comment