Wednesday, August 13, 2014

Changing list view properties in Listview webpart Programmatically

Introduction

As well know about views in Lists and Listview webpart which is used in sitepages/pages. Here i am going to share an idea about changing views of listview webpart programmatically.

ListView Webpart

We can use listview webpart to display and edit list items in Sitepage/Page. It displays information in different ways for different purpose such as filtering, sorting / selecting specific columns. Its a short note about listview webpart. Now, Let's see how to add list view webpart programmatically.

Adding Listview Webpart


Changing properties of view in Listview Webpart

We are going to access the webparts in a page using SPLimitedWebPartManager. Then, Get the webpart by using condition check with webpart title. Convert the webpart as XsltListViewWebPart. Take view from it and change as we want. For instance, I have changed the number of rows in listview. 


  web.AllowUnsafeUpdates = true;

        XsltListViewWebPart webPart = null;
        SPLimitedWebPartManager wpManager = SPContext.Current.Web.GetLimitedWebPartManager("your page name", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpManager.WebParts)
        {
          if (wp.Title.Equals("Webpart title"))
                    {
webpart = wp as XsltListViewWebPart;
var currentView = webPart.View;
                currentView.RowLimit = Convert.ToUInt32(3);
                currentView.Update();
                web.AllowUnsafeUpdates = false;

}
}

This is code to change row limit. we may change some of the properties in View. That's it.

No comments:

Post a Comment