Thursday, August 7, 2008

Create custom ListViewWebPart

Hi All,

First of All Special Thanks to

Arun

Creating ListViewWebpart programmatically

http://www.sharepointu.com/forums/p/2141/5876.aspx

Guzel

http://forums.msdn.microsoft.com/en-US/sharepointdevelopment/thread/e7eda00c-14c7-41f9-87b1-09d911c30378/


Basics can be found @ :
http://www.sharepointplatform.com/teamblog/Lists/Posts/Post.aspx?List=427bfca2%2Db731%2D4c19%2D87c6%2D83c90460e02c&ID=42

These sites and people have helped us in this venture.
We are great full to all those on the internet, who apply their thoughts and provide with such good codes and tips.
We sincerely apologize if we forgot to mention any one over here.



Now to the venture: Custom ListViewWebPart

Recently one of the projects in which we are involved needed to develop a Web Part.
This Web Part had to look same as the OOTB (Out Of The Box) DataView.

The requirment further stated that it should have the Following:

1) Show Data from any List.
2) Show Data from any View applied on the List.
3) Show Data from any List with Dynamic Queries.
4) Show be able to sort,filter,edit,add new item etc on the List.

:) There was a never ending List of requirments that the client gave us.


We Created this Web Part from the Base
Microsoft.SharePoint.WebPartPages.ListViewWebPart

he Parameter that Make it Fully Dynamic are in the
Tool Panel --> Miscellaneous

Below are the Images that will give you the exact Picture:








The Source Code of the entire Web Part is here:


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Security.Principal;




namespace Com.SharePointKings.Webparts.Com.SharePointKings.Webparts
{


[Guid("5BAC68E6-0B8C-4b0b-85B7-7D8340480D5C")]
public class CustomListViewWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
#region Variable Declaration
private string strlisttolink = string.Empty;
private string strViewOfSourceList = string.Empty;
private string strQuery = string.Empty;

#endregion
#region Properties

//Get and Set Property of the Source List
[Personalizable(true),
WebBrowsable(),
WebDisplayName("List Name"),
WebDescription("Pass the Name of the List")]
//Get and Set Property of the Source List
public string ListToLink
{
get
{
return strlisttolink;
}
set
{
strlisttolink = value;
}
}

[Personalizable(true),
WebBrowsable(),
WebDisplayName("View"),
WebDescription("Pass Name of the View that you will like to apply to the List")]
//Get and Set Property of the Source List
public string ViewOfSourceList
{
get
{
return strViewOfSourceList;
}
set
{
strViewOfSourceList = value;
}
}
[Personalizable(true),
WebBrowsable(),
WebDisplayName("Query"),
WebDescription("Pass the Filter Query")]
//Get and Set Property of the Source List
public string FilterQuery
{
get
{
return strQuery;
}
set
{
strQuery = value;
}
}

#endregion

public CustomListViewWebPart()
{
this.ExportMode = WebPartExportMode.All;
}



protected override void CreateChildControls()
{
base.CreateChildControls();

try
{

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[ListToLink];



ViewToolBar toolbar = new ViewToolBar();

SPContext context = SPContext.GetContext(this.Context, list.Views[ViewOfSourceList].ID, list.ID, SPContext.Current.Web);

toolbar.RenderContext = context;

Controls.Add(toolbar);




// Instantiate the web part
ListViewWebPart lvwp = new ListViewWebPart();
lvwp.ListName = list.ID.ToString("B").ToUpper();
lvwp.ViewGuid = list.Views[ViewOfSourceList].
ID.ToString("B").ToUpper();

SPView webPartView = web.Lists[ListToLink].Views[ViewOfSourceList];
SPList objList = web.Lists[ListToLink];
webPartView.Query = FilterQuery;

// Remove the Toolbar!
// First Option: Do it throught OOTB.This needs to be done from the VIEW of the Web Part
// Second Option: Do it trhough Coding.This line is required to ensure that all the appropriate internal nodes of the SPView are populated

String temp = webPartView.SchemaXml;
System.Reflection.PropertyInfo ViewProp = lvwp.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
SPView spView = ViewProp.GetValue(lvwp, null) as SPView;
// This forces a refresh of the views internal xml or the node's cild nodes are not populated


PropertyInfo nodeProp = webPartView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
XmlNode node = nodeProp.GetValue(webPartView, null) as XmlNode;

// Now get the Toolbar node from the view so we can update its type property
XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
if (toolbarNode != null)
{
toolbarNode.Attributes["Type"].Value = "None";
web.AllowUnsafeUpdates = true;
webPartView.Update();
web.AllowUnsafeUpdates = false;
}
//End Remove the Toolbar!

web.AllowUnsafeUpdates = true;
webPartView.Update();
objList.Update();
web.AllowUnsafeUpdates = false;
lvwp.GetDesignTimeHtml();
this.Controls.Add(lvwp);
}
catch (Exception ex)
{
Label lbl = new Label();
lbl.Text = "Error occured: ";
lbl.Text += ex.Message;
this.Controls.Add(lbl);
}
}


protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}
}
}

21 comments:

Ryan said...

I've almost got this up and running, however there is a part in the code that says "//remove the toolbar!" yet there is no code underneath it actually removing the toolbar.

How did you remove the toolbar? I tried setting the webPartView.Toolbar property to "None" or "" but it didn't work, I can't really even see where the webPartView is tied to the lvwp.

Manoj Iyer said...

Hi Ryan,

Yes we tried to remove the ToolBar the same way as you did, but it didn't work. So we remove the code.

Now there is another way you can get rid of the Extra ToolBar that you see.

Go to the List in Question. When you do an Edit Page and Click on "Modify Shared Web Part" on the Default All Items Page of the List.

In the List View ToolPane on the Right Hand Side, Select the View that you are going to give as the Property for our WebPart, select "No ToolBar" in the ToolBar Type: Property.

This will Remove the ToolBar.

No go to the Page where you have your CustomListViewWebPart. Just refresh the Page.

This will get you rid of the ToolBar.

I hope we have answered your query. Thanks a Lot for your supprot.

-SharepointKings

Manoj Iyer said...

Hi Rayn,

In reply to your query of Removing the Toolbar! we have something for you. This chunk of code with do it smoothly.


// Remove the Toolbar!
// First Option: Do it throught OOTB.This needs to be done from the VIEW of the Web Part
// Second Option: Do it trhough Coding.This line is required to ensure that all the appropriate internal nodes of the SPView are populated

String temp = webPartView.SchemaXml;
System.Reflection.PropertyInfo ViewProp = lvwp.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
SPView spView = ViewProp.GetValue(lvwp, null) as SPView;
// This forces a refresh of the views internal xml or the node's cild nodes are not populated


PropertyInfo nodeProp = webPartView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
XmlNode node = nodeProp.GetValue(webPartView, null) as XmlNode;

// Now get the Toolbar node from the view so we can update its type property
XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
if (toolbarNode != null)
{
toolbarNode.Attributes["Type"].Value = "None";
web.AllowUnsafeUpdates = true;
webPartView.Update();
web.AllowUnsafeUpdates = false;
}
//End Remove the Toolbar!


Rayn, We appreciate your curiosity and are looking forward for more Queries

Alex Groff said...

It can be easy done by using ListView/ListViewByQuery/ViewToolbar controls from Microsoft.SharePoint.WebControls without reflection.

Chris Buchanan said...

There's another post detailing how to change the toolbar types between Standard, FreeForm and None located at http://spschris.blogspot.com. Check it out.

Chris Buchanan
Solution Developer
www.redsphere.ca

David said...

Hi all!

I´m trying to develope a webpart like this. But, my first doubt. is it possible to develop de LisViewWebPart for list from other sites or subsites? Even other site collection, this is my customer´s requirements. Thanks in advance and nice post!

Parth Patel said...

Hi Devid,

good requirement, we had not tried with different site collection, subsite or other site but if you can check the code then list view webpart is bind with list and its view. so it should work theoritically. We will try if we get time to work on this requirement.

And if you tried then please share your experience weather its working or not.

dirq said...

Thank you for the code. It's a big help so far. I have the same requirements as David - I need to pull data from a list in the parent web site. I've switched the list to use the parent list but I've been getting an error that originates in the lvwp.GetDesignTimeHtml() call. It's rather lengthy so I'll just give you the basics:

List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user.It seems that the internal call to "EnsureData(0)" is not able to find the list. The internals of EnsureData is obfuscated so I can't look at the code in Reflector.

Any ideas? Has anyone created a list view of items from another site? I need the rendered web part to look like the standard lists.

Thanks a lot for your help,

Dirk Watkins

Mark Stokes said...

Awesome. At last a post that works post Infrastructure Updates.

I have created my own post based on this here:

http://sharepointstudio.com/People/MarkStokes/Lists/Posts/Post.aspx?ID=27

Anonymous said...

When using a query, the query works! However, when you use the toolbar actions to export to spreadsheet, it displays all items instead of the filtered items. Is there a way to get the export to only export the filtered items?

SharePoint Kings said...

Mark,

Nice effort, Thanks for sharing we will check it and revert soon.

SharePoint Kings said...

To Anonymous,

its basic behavior of list view, and out of the box web part is also behaving the same. so this is expected behavior

Todde said...

I've almost got this up but its not running. Is the code wrong. What is my failure.

SharePoint Kings said...

Todd,
what's the error?
where are you stuck?

Mado said...

The key to getting the ListViewWebpart to work for lists on a different level:

Set the WebId property of the ListViewWebpart to the web.ID the list is on.

Anonymous said...

Hi,

Excellent code...How to get different views from subsites and other site collections.How to do that?

SharePoint Kings said...

sorry for subsite thing,
still not done that.

gus said...

Hi folks,

thanks a lot for the useful code. I have a question: even if I choose the "Project Tasks" view of a Projects list I created, and I link it to the custom Web part via its miscellaneous properties, I cannot manage to have the Gantt chart to appear: I just get the normal tabular list of the tasks. Any ideas?

Many thanks

Lorenzo

SharePoint Kings said...

gus, no idea about programmatically

for gant chart we have to use SPListTemplateType.GanttTasks somewhere

nice thing to investigate, will try to get back to you soon.

divyesh said...

Hi,

I have two ListViewwebpart on same page. If I drill down to any folder of one webpart the other webpart shows the content of rootfolder. For an example in First webpart I drill to Level 1 folder and then i thought drill down in second webpart. The page gets post back and first webpart displays the contents of rootfolder and not the Lvel1 folder.

Any idea on this behavior of Listwebpart.

Please mail me on divyesh.kotadia@bsil.com.

Thanks,
Divyesh

SharePoint Kings said...

divyesh,

yes able to get same behavior, as you are getting.

don't know about this behavior.
need to research on that.
will try to get back to you soon.

Thanks for sharing.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com