为特定 SharePoint 列表添加自定义上下文菜单项






4.89/5 (3投票s)
使用功能为 SharePoint 列表添加自定义上下文菜单
引言
如果您曾经尝试将 SharePoint 自定义操作添加到列表操作菜单,并尝试在“RegistrationId
”中指定特定的列表或内容类型,但它不起作用。SharePoint 会静默地不呈现您的自定义操作。如果您尝试使用“RegistrationId
”为“100
”来定位特定的列表,您会发现 SharePoint 会将您的操作呈现在网站的每个列表上。以下是针对此问题的解决方案。
Using the Code
按照以下步骤实现所需输出
- 在 Sharepoint 14 Hive 功能文件夹下创建一个新文件夹,并将其命名为 OrderDetails(订单详情)。
- 在 OrderDetails 文件夹下创建一个新的 Feature.Xml 文件,并将以下代码粘贴到 XML 文件中。
<?xml version="1.0" encoding="utf-8" ?> <Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="{AAD5B340-D5FC-4044-AA85-47528F137192}" Description="This feature contains a custom action that launches the Edit Order Details Page" Hidden="False" Title="Edit Order" Scope="Web" Version="1.0.0.0"> <ElementManifests> <ElementManifest Location="Default.xml"/> </ElementManifests> </Feature>
请确保生成一个新的 GUID,并使用您的新 GUID 更新上述 ID。
- 在 OrderDetails 文件夹下创建一个新的 Default.xml 文件,并将以下代码粘贴到 XML 文件中
<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction Id="ComprehensiveModifyItem" Location="EditControlBlock" Title="Edit Order" RegistrationType="ContentType" RegistrationId="0x01003A034A8880077243A36E17993E95DCB7"> <UrlAction Url="~site/Lists/Shared%20Documents/EditOrderDetails.aspx?ID={ItemId}" /> </CustomAction> </Elements>
请确保将 EditOrderDetails.aspx 替换为您的自定义页面。这里,Registration ID 是列表内容类型 ID。要获取列表内容类型 ID,请按照以下步骤操作
- 转到列表设置 --> 高级设置 --> 将允许管理内容类型设置为“是”。
- 列表设置 --> 内容类型部分 --> 单击文档内容类型 --> 从 URL 中获取
ctype
值。 在我的例子中,ctype=0x01010045A5D7FD06EBEC4B925C89A76834F101
。 - 将此
ctype
值更新到上述 Default.XML 文件中作为RegistrationID
属性。
- 使用以下 PowerShell 命令安装上述功能
Install-SPFeature OrderDetails
- 使用以下 PowerShell 命令启用该功能。我们也可以使用
stsadm
命令启用该功能。Enable-SPFeature OrderDetails -Url "http://SharepointPortal/CustomerOrders"
这里
CustomerOrder
是Subsite
(子站点)。 - 上述功能仅对 Registration ID 中指定的 Sharepoint 列表共享文档启用。
完成以上所有步骤后,您将在共享文档列表中的每个列表项中看到一个新的菜单项 编辑订单。 如果您注意到其他列表,您将看不到此上下文菜单项。
要禁用和卸载该功能,请使用以下命令。 使用 -force
选项强制安装。
Disable-SPFeature OrderDetails -Url "http://SharepointPortal/CustomerOrders"
UnInstall-SPFeature OrderDetails