Aug 15, 2008

Add the SharePoint List Item's Context Menu to any Column with PowerShell

With this PowerShell script you can add the list item's context menu to any column you want. You'll find the script (AddMenuColumn.ps1) on my Codeplex Project iLoveSharePoint.

I'll just give you an example how to use:

  • Let's assume you have a calculated list column called "justAnyColumn" with the following formula "[Title]&[Content Type]"

image

  • In this scenario we assume you don't need the title column, instead you want to show the "justAnyColumn" to select an entry. But unfortunately the context menu is attached to the title column. And now?!

image

  • We could show the "Edit (link to edit item)" column in the view.image
  • May be a solution - But our users are accustomed to use the context menu. Well!
  • Now you can use my script:
  • Start the PowerShell
  • Run my script as follows:
  • PS> & .\AddMenuColumn.ps1 -webUrl http://[server:port]/site/subsite/ -listName "Test" -fieldTitle "justAnyColumn" -addToDefaultView $true
  • Let's take a look at the result:

image

  • Now you've got a new column, containing the value of "justAnyColumn" with attached context menu.
  • That's all!

How to remove the column?

  • Because the column is read only in the UI, you can't delete there.
  • Instead use my iLovePowerShell script:
    • PS> $web = get-spweb http://[server:port]/site/subsite/
    • PS> $list = $web.Lists["Test"]
    • PS> $field = $list.Fields["justAnyColumn"]; #better would be to use the Guid of the field, because the field name exists twice
    • PS> $field.ReadOnyField = $false
    • PS> $field.Update()
    • PS> $field.Delete()
    • PS> $web.Dispose()

Hope somebody can use this...

Bye,

Christian

59 comments:

Dinesh (dinid@yahoo.com) said...

Hi, I thank you for your post. It really saved me a lot of time. I have a question, Can i use your script for a document Library to change the context menu. I tried to use it in a document library and i get the Context menu to another column but it displays "render Error" in the along with the document name in the column.

Thank you in advance for your help.

Christian said...

Hi Dinesh, i fixed that. Download the actual script from codeplex. Unfortunately you have to delete the old menu column with the render error. Please use the ID to delete using title deletes first the source column. Find the ID via "$list.Field | format-table Title, AuthoringInfo, ID" The one wit the authoring info "with menu" is the right one.
Bye,Christian

dinesh said...

Hi Christian,
Thanks much for all your help. Your code has helped me and my colleague to resolve a solution for in one of our project.
Thanks again,

Christian said...

Hey Dinesh,
you're welcome. I'm happy for any feedback or idea of approvment.
Regards,
Christian

Reeja Sorex said...

Hi Cristian,
Yours was an excellent post.I have implemented it,but have a doubt.When I try to hide the original justAnyColumn,the text disappears from the justAnyColumn with context menu.How to resolve it .Thank You once again for your post.

Christian said...

Hi Reeja Sorex,
you're right. I just fixed that. Please download the latest version.
Thanks for your feedback.
Bye, Christian

Reeja Sorex said...
This comment has been removed by the author.
Reeja Sorex said...

Hi Christian,
Thanks for the quick response,its working like magic! Continue your good work.
Bye, Reeja

Anonymous said...

It works great...and is exactly what we were after. Thanks.

One thing is though...When the new LINKED column is clicked (not context menu just clicking the href) we would like to be prompted with the "Read Only or Edit" prompt.

Is this possible?

I tried changing the onclick event in your script file to call the DispEx()...JS method with no luck.

Christian said...

Hi,
I think you're using a DocLib and want show the default SharePoint dialog. Should be possible. Have look to the MS rendering pattern in the 12\TEMPLATES\XML\field_types.xml and the list definitions in the feature folder for inspiration.
Bye, Christian

111 said...
This comment has been removed by a blog administrator.
Anonymous said...

I got this message when trying to execute the script:

Unable to index into an object of type Microsoft.SharePoint.SPListCollection.
At D:\Srvapps\DEVELOPMENT\~DOWNLOADS\SP Powershell\AddMenuColumn.ps1:16 char:20
+ $list = $web.Lists[$ <<<< listName];


Any Ideas??

Christian said...

The error indicates that the list on the specfied web can not be found. The -listName and -fieldTitle params are case sensitive.
Bye, Christian

Ledlincoln said...

I used your script and it appeared to work fine until I discovered that it broke the "Export to Spreadsheet" action. The modified column no longer gets exported. All other columns do appear in the resulting Excel spreadsheet.

ACrush said...

Unfortunately, the case sensitivity does not explain every case where you get the "unable to index into an object of type microsoft.sharepoint.sp..." error. I have encountered a situation where identically referenced (the field title is in Russian, but you can check the referrals are identical; actually I used the "up" key to avoid mistyping the title) indexer calls succeed and fail without any apparent reason.

E.g.

PS C:\Documents and Settings\admin>>> $target["Дирекция"]=8
PS C:\Documents and Settings\admin>>> $item["Дирекция"]
PS C:\Documents and Settings\admin>>> $item["Дирекция"]=$null
Unable to index into an object of type Microsoft.SharePoint.SPListItem.
At line:1 char:7
+ $item[" <<<< Дирекция"]=$null
PS C:\Documents and Settings\admin>>> $target["Дирекция"]
8

ACrush said...

And don't say the error is because I try index into $item, not $target - it doesn't matter:

PS C:\Documents and Settings\spservice2007>>> $target["Дирекция"]=$null
Unable to index into an object of type Microsoft.SharePoint.SPListItem.
At line:1 char:9
+ $target[" <<<< Дирекция"]=$null
PS C:\Documents and Settings\spservice2007>>> $target["Дирекция"]
8

Christian said...

Hi AChrush,

sorry never had this kind of trouble. Sounds weird.

Bye, Christian

ACrush said...

OK, I did some more testing and I find that the error message is misleading here. The <<< indicator points to the wrong place in the error generating string. The problem arises when you actually try to assign an empty value like $null to a lookup. So the message should be "Unable to set null value" rather than "Unable to index...".

Romain said...

Hi and thank you !
I've taken a look on your source code and more particulary on AddMenuColumn.ps1
I have created a custom field and i want to use the same display pattern to add the context menu (in FLDTYPEs_xxx.xml).
The menu works but I have an error with the "Show element" option.
Have you any idea to add the menu in the XML file instead of use your script.

Thank you in advance for your help. (and sorry for my poor English ;))

Jasper said...

I got everything working, but the behaviour for document libraries isn't 100% correct.

In the default context menu, both clicking on the link as choosing the option 'Edit in Word' (for Word documents) would open a popup asking me if I would want to edit the document or open as read-only.

Now when I click either of those options, the document opens as read-only in Word and Word doesn't recognize the document is server based (normally a "check-out document" button would appear).

Is it possible to alter the command so this becomes possible again?

Christian said...

Hi Jasper,

yes it should be possible.
Lists and Document Library have a different context menu rendering pattern. Take a look in the Doclib List Definition from Microsoft in the 12 hive folder. Copy the rendering pattern.

Bye, Christian

Jasper said...

Hi Christian,

Could you be a little more specific on what I should replace and where?

Anonymous said...

Could any one help me that how I can remove Edit Item in List Item's Context Menu?

Swaminathan said...

Hi Christian,

I am looking for a solution to link the title column to the document in a document library.

any solutions available. Please help!

Swami

Anonymous said...

You save my life man, thank you SO much!.
Also, Based on yours I've built something to add a link column (open file, no menu). So people can open document library files by using a link that displays the content from any column other than just "Title" or "Name". Let me know if you need it. axkan@hotmail.com

Anonymous said...

Hi Christian,
Any plans on fixing the AddMenuColumn.ps1 script to work on a SP2010 installation?
Many thanks, it works beautfully on SP2007
Regards.

Christian said...

Actually not, there are many other features I've to upgrade first. Have you try it out with SP2010? I've thought that I've seen something similar in SP2010 out of the box, but haven't found it in SP2010 RTM, maybe I'm wrong.

Anonymous said...

I tried on SP2010... with sum editing but I couldnt make it work.
acctually it isnt needed anymore... sharepoint designer made things really easy.
regards

Steve Agius said...

We are trying to run the script and getting the following error:

PS C:\> & .\AddMenuColumn.ps1 -webUrl
http://mcanet-test/Units/InformationManagement/imcontacts/ -listName
"Companies" -fieldTitle "Address" -addToDefaultView $true

Cannot index into a null array.
At C:\AddMenuColumn.ps1:33 char:29
+ $sourceField = $list.Fields[ <<<< $fieldTitle];
+ CategoryInfo : InvalidOperation: (Address:String) [], ParentCon
tainsErrorRecordException
+ FullyQualifiedErrorId : NullArray

Any help will be greatly appreciated.

Christian said...

Steve, "Cannot index into a null array" indicates that either the list or the field has not been found.
Bye, Christian

Steve Agius said...

Christian,

Many thanks for the reply.

I am still getting the same error message.

Below are the details:

URL is: http://mcanet-test/Units/CorporateServices/Lists/Telephone%20List/AllItems.aspx

and this is the command that I am trying to execute:

PS C:\> & .\AddMenuColumn.ps1 -webUrl http://mcanet-test/Units/CorporateServic
es/ -listName "Telephone%20List" -fieldTitle "Extensions" -addToDefaultView $tru
e

Could you kindly help

Christian said...

Hi Steve,
try to use "Telephone List" instead of "Telephone%20List".
Bye, Christian

Steve Agius said...

Tried it as well. Still didn't work. Any more ideas??

Christian said...

Hi Steve, please double check that you use the title of the list for the listName name parameter. For trouble shooting you can select each single object with the following script: http://cglessner.blogspot.com/2008/06/powershell-sharepoint.html
Bye, Christian

Isabel said...

Christian,
I can confirm that it does NOT work on SP 2010.
If you have a chance I would appreciate if you can write the version for SP 2010.
Thanks,

mkraus81 said...

i tried this script for sharepoint 2010, but it does not work
Has anybody an idea to modified this script for SP 2010?

mkraus81 said...

script works with sp2010, but the new column is empty, is this normal?

turtlehand said...

This rocks, thanks. We had given up on adding the ECB to other columns until I found your complete post.

Thanks again, Joe

Soxiz said...

I can also confirm that this does not work on SharePoint 2010. The column is added but is empty. IF you could write a version for 2010 it would be great :)

Slinky911 said...

Hi Christian ... Your code is Awesome, it solved one of my major headaches. But i've noticed that when i open in Datasheet View, the Column Disappears?? Any ideas?
Thank you.

Luis Felipe Herrera Orejel said...

Hi Christian!

I'm getting this error while running your script;

You cannot call a method on a null-valued expression.
At C:\Users\luis.felipe\Desktop\AddMenuColumn.ps1:21 char:26
+ $web.Locale.LCID.ToString <<<< ()
+ CategoryInfo : InvalidOperation: (ToString:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull

do you have any clue??

Christian said...

Hi Luis,
maybe the web is null. That could be caused by a wrong url or maybe your user has no access to the web or no access to SharePoint database. When you run PowerShell scripts the current user will be used for the connection to the SharePoint database.
Bye,
Christian

cewitail said...
This comment has been removed by a blog administrator.
Anonymous said...

Steve,
Did you ever resolve your issue. We are experiencing the same thing and after triple checking what I entered, I am sure it is all correct.

Christian said...

Hi, -fieldTitle refers to the title (display name) of an existing column. -listName is the title (display name) of the list/doc lib. Anyway, I've never tested it with sharepoint 2010.
Bye,
Christian

Chris Webb said...

Guys, for SharePoint 2010, it's super easy! Read this post, I found it, the first paragraph about turning it on using SharePoint Designer. Quick and easy.
http://blogs.msdn.com/b/chunliu/archive/2010/09/27/enabling-ecb-menu-on-a-custom-column-in-sharepoint-2010-part-1.aspx

Mr Bello said...

Christian, Thanks for your contribution. It has been of great help. Question... do you have a similar script but for doing a 'link to item' instead of the edit menu?
Thanks again!

Rajiv said...

Hii..
I just want to say that this works on Sharepoint 2010 but the calculated field doesnt not show the data when its on display in the list but it shows the data when each row is viewed on the individual item "DisplayItem" view.

Aaron said...

How do you reference a field via its GUID?

Christian said...

$list.Fields[[Guid]"826BD4FC-C6BD-4C49-A748-B3C7E2D30A28"]

Sandeep Jha said...

Hi Cristian,
Excellent post.
year back ,i have implemented in one of the MOSS 2007 project , which works grate.
I am trying the same with SharePoint 2010.I modified the file .PS1 file replaced "14" instead of 12 hive. I have implemented it with “Title” column of document library, but the text disappears from the "Title(with menu)" also the context menu does not appear. Where was default “title” has text.
How to resolve it.

Christian said...

Hi, have you already tried to add the context menu via SharePoint Designer: http://blogs.msdn.com/b/chunliu/archive/2010/09/27/enabling-ecb-menu-on-a-custom-column-in-sharepoint-2010-part-1.aspx

Bye,

Christian

Anonymous said...

For SP2010: just add ListItemMenu="TRUE" LinkToItem="TRUE" to your fieldrefs.

Sandeep Jha said...
This comment has been removed by the author.
Sandeep Jha said...

Hi Cristian,

I have already tried to add the context menu via SharePoint Designer.it works grate with a little problem when we use this feature on a column in the document library (1. Edit in undefined ,2. Edit in Microsoft Word), which is also mentioned in the link which you shared here(http://blogs.msdn.com/b/chunliu/archive/2010/09/27/enabling-ecb-menu-on-a-custom-column-in-sharepoint-2010-part-1.aspx)

Thanks

also , i already added these properties to field ref , but text not appears for the column.
For SP2010: just add ListItemMenu="TRUE" LinkToItem="TRUE" to your fieldrefs.

Anonymous said...

Any Resolution to Sandeep's issue.

I am running into the same thing, no resolution and SP Designer is not the solution.

Anonymous said...

Hi,

I want to remove the Alertme,Delete items in list item context menu.
Any possible solutions?

Anonymous said...

Hello,

is there any way when we click on the item in doc library that open in office web apps, and not in display forms.

thanks,

Anonymous said...

Hello,

is there any way to open item in Doc library in office web apps, and not in display forms.

thanks,