in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

This Blog

Syndication

ChadClarkesMOSSBlog

Custom Web Part Code: Adding the default doc lib menu to a custom web part

So I found a way to add the default drop down menu to the items of a custom web part control.  Its pretty convoluted, so if you form a more direct way let me know.  See code below:

This Template is needed to be instantiated, but we don't render it... (lil trick)

msaListMenu = new MenuTemplate();
msaListMenu.ID = "DocLibMenu"; 

I use this menu column to expose multiples values I need for the menu:

SPMenuField msaColMenu = new SPMenuField();
msaColMenu.HeaderText = "Name";
msaColMenu.TextFields = "FileLeafRef, ID, COUID";
//msaColMenu.TextFormat = "{0}/{1}/{2}";
msaColMenu.MenuTemplateId = "DocLibMenu";
//msaColMenu.
msaColMenu.NavigateUrlFields = "FileLeafRef";//Client_x0020_Name_x0020_2";
msaColMenu.NavigateUrlFormat = SPContext.Current.Web.Url+"/[Doc Library Name]/1/{0}";//1
msaColMenu.TokenNameAndValueFields = "EDIT=ID,NAME=Title,TYPE=DocIcon,REF=FileLeafRef";
msaColMenu.SortExpression = "Title";
msaGrid.Columns.Add(msaColMenu);

Javascript I implanted directly after the rendering of the SPGridView Control (this javascript manipulates the menu field that we put in our SPGridView control and gives it the identical html that the default web parts use for their menus:

string transplantMenuFunction = "";
transplantMenuFunction += "<script>\n";
transplantMenuFunction += "function transplantMenu(){\n";
transplantMenuFunction += "   var error;\n";
transplantMenuFunction += "   try{\n";
transplantMenuFunction += "     var msaGridView = document.getElementById('" + msaGrid.ClientID + "');\n";
transplantMenuFunction += "     var spans = msaGridView.getElementsByTagName('span');\n";
transplantMenuFunction += "     for(var j = 0; j < spans.length; j++){\n";
transplantMenuFunction += "         if(spans[j].title == 'Open Menu'){\n";
transplantMenuFunction += "             //t = t+1;\n";
transplantMenuFunction += "             var valuesString = spans[j].children[0].children[0].children[0].children[0].children[0].innerText;\n";
transplantMenuFunction += "             var values = valuesString.split('/');\n";
transplantMenuFunction += "             spans[j].innerHTML = '<table height=\"100%\" cellspacing=0 class=\"ms-unselectedtitle\" onmouseover=\"OnItem(this)\" CTXName=\"ctx1\" Id=\"'+values[1]+'\" Url=\"" + SPContext.Current.Web.Url + "/" + SPContext.Current.List.Title + "/1/" + "'+values[0]+'\" title=\"" + SPContext.Current.Web.Url + "/" + SPContext.Current.List.Title + "/1/" + "'+values[0]+'\" DRef=\"" + SPContext.Current.Web.Url.Replace(SPContext.Current.Site.Url+"/", "") + "/" + SPContext.Current.List.Title +"/1" + "\" Perm=\"0x7fffffffffffffff\" Type=\"\" Ext=\"\" Icon=\"icgen.gif||\" OType=\"0\" COUId=\"'+values[2]+'\" HCD=\"\" CSrc=\"\" MS=\"0\" CType=\"Item\" CId=\"0x010051390B24CF204947B3B8CFAA6DADB0BF\" UIS=\"512\" SUrl=\"\"><tr><td width=\"100%\" Class=\"ms-vb\"><a onfocus=\"OnLink(this)\" href=\"" + SPContext.Current.Web.Url + "/" + SPContext.Current.List.Title + "/1/'+values[0]+'\" ONCLICK=\"GoToLink(this);return false;\" target=\"_self\">'+values[0]+'<!--<IMG BORDER=0 ALT=\"'+values[0]+'\" title=\"'+values[0]+'\" SRC=\"/_layouts/images/icgen.gif\">--></a></td><td><img src=\"/_layouts/images/blank.gif\" width=13 style=\"visibility:hidden\" alt=\"\"></td></tr></table>'\n";
transplantMenuFunction += "             \n";
transplantMenuFunction += "         }\n";
transplantMenuFunction += "     }\n";
transplantMenuFunction += "   }\n";
transplantMenuFunction += "   catch(error){\n";
transplantMenuFunction += "     alert(error.message);\n";
transplantMenuFunction += "   }\n";
transplantMenuFunction += "}\n";
transplantMenuFunction += "transplantMenu();\n";
transplantMenuFunction += "</script>\n";
writer.Write(transplantMenuFunction);

Comments

 

Paul Turner said:

October 6, 2007 2:25 AM
 

Chad Clarke said:

Seen this link, and its great!  However, you'll have to implement your own security model and recreate default sharepoint functionality...  I actually created this method as an alternative to using a custom menu...

October 7, 2007 5:12 PM
 

dmitryvy said:

Hi Chad!

This is great article - I have been looking for this for days and you are the only one who menioned this issue!!! But unfortunately I failed to get it working - could you please post the whole solution?

Many Thanks

October 16, 2007 6:30 AM
 

ChadClarke said:

Sadly, my client did not want to utilize the default menu, so I created a custom menu, and I did not save the working sample of code...  I apologize.  If you would like additional help with your specific situation, you can contact me at chad.clarke@lrs.com.  

October 16, 2007 11:03 AM
 

Chad Bostick said:

Our team is working on some custom web parts that display data that looks and acts like a Sharepoint list, but is coming from other sources. I’ve been looking for a way to render a hover-over/dropdown menu per item like normal Sharepoint lists do (but with custom menu actions). This article looks like it might be a great start to get it working. Looks like you wrote it a few months ago, just wondering if you’ve found any better ways to accomplish this since then or if this solution ended up working for you. I tried emailing you at the address in the above comment but it got bounced.

Thanks,

Chad Bostick

February 5, 2008 1:58 PM
 

ChadClarke said:

Sorry, I've switched companies.  My new email is Chad.Clarke@us.sogeti.com.  Yes, this strategy ended up working.  However it relied on a default list's menu.  In your case it seems as it you'll need your own spmenu.  In that case you won't need to utilize this work around.  Just implement SPMenutemplate (don't forget to render it) and attach it to the SPMenu.  I have been out of web part development for a bit of time now, so class name stated in this comment may be inaccurate, but for a speedy response I went ahead and remembered what I could.  My latest endeavor is working with authentication models, error log securing, back up and restore models, and more deployment type work.  I appreciate the post and feel free to email me if you have questions.  I'll dig up anything you need.  Sounds like you got a great project there, wouldn't mind putting in my two cents.  :)

February 7, 2008 9:38 AM
 

JayZeh Cristobal said:

Hey,

Is it posible to recreate those New->Item on top of SPGridView just like those OOTB list web parts when displayed?

April 4, 2008 1:41 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts