Programmatically create a view (custom view) of a list and change the view

If you want to programmatically create a view of a list and if you want to change the existing view of a ListViewWebPart to the custom view you can develop it by referring the following code sample.

code snippet for creating a custom view

=====================================

SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL

SPWeb oWeb = oSite.OpenWeb();

SPList oList = oWeb.Lists["shared documents"];

SPViewCollection oViewCollection = oList.Views;

            string strViewName = "MyCustomView";

            System.Collections.Specialized.StringCollection viewFields =

            new System.Collections.Specialized.StringCollection();

            viewFields.Add("Name");

            viewFields.Add("Type");

         

            string query = "<Where><Eq><FieldRef Name=\"Name\"/>" +

                "<Value Type=\"Text\">mysample</Value></Eq></Where>";// here you can filter your items using the selected

                                              item in the dropdownlist

            oViewCollection.Add(strViewName, viewFields, query, 100, true, false);

 oWeb.Update();

Now refresh the list in UI and then you can see the “MyCustomView” in the selection list of views.

The below code will change the current view of list to our custom view. You can use the below code to change the view programmatically.

code snippet for changing the view

======================================

try

            {

    SPSite oSite = new SPSite([Site URL]);// change it to your sharepoint site URL

                SPWeb oWeb = oSite.OpenWeb();

                SPList oList = oWeb.Lists["shared documents"];

                SPLimitedWebPartManager oWebPartManager = oWeb.GetLimitedWebPartManager("Shared%20Documents/MyCustomPage.aspx", PersonalizationScope.Shared);// change the wepart page absolute URL to your webpart page URL

                foreach (System.Web.UI.WebControls.WebParts.WebPart oWebpart in oWebPartManager.WebParts)

                {

                    if (oWebpart.GetType().Name == "ListViewWebPart")

                    {

                        ListViewWebPart myWebPart = (ListViewWebPart)oWebpart;

                        SPView doclibview = oList.Views[“MyCustomView”];

                        Guid oGuid = new Guid(myWebPart.ViewGuid);

                        SPView oWebPartView = oList.Views[oGuid];

                        oWebPartView.ViewFields.DeleteAll();// deleting the existing view fields for adding new one

                        foreach (string strField in doclibview.ViewFields)

                        {

                            oWebPartView.ViewFields.Add(strField);

                   }

                        oWebPartView.Query = doclibview.Query;

                        oWebPartView.RowLimit = doclibview.RowLimit;

                        oWebPartView.ViewEmpty = doclibview.ViewEmpty;

                        oWebPartView.ViewFooter = doclibview.ViewFooter;

                        oWebPartView.ViewHeader = doclibview.ViewHeader;

                        oWebPartView.Scope = doclibview.Scope;

                        oWebPartView.GroupByFooter = doclibview.GroupByFooter;

              oWebPartView.GroupByHeader = doclibview.GroupByHeader;

                        oWebPartView.Update();

                        myWebPart.ViewGuid = oWebPartView.ID.ToString("B").ToUpper();

                        myWebPart.Visible = true;

       myWebPart.Title = "My title";

                        oWebPartManager.SaveChanges(myWebPart);

                        break;

                    }

                }

                oWeb.Update();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }