您的位置:首页 > 其它

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer

2010-04-25 09:53 579 查看
关于SharePointExplorer,是VS2010的一个新特性,可以让开发人员很方便地浏览到SharePoint站点的结构,并且做一些简单事情

我在下面这篇文章中已经做过一些介绍

/article/4660852.html

上文中,我已经提到了,这个工具我认为还不是很完善。但它确实留出了定制的空间。那么今天我们就来试一下吧

为什么我会想做这个事情呢,就是因为我在本系列的第10篇中提到了内容类型的开发,我们需要查看某个内容类型的定义的话,比较费劲,需要特意去写代码。所以我就想,如果直接在这个Explorer中能做这个工作,当然比较好。然后就手痒无比,马上动手做了起来。

1.首先来看一下SharePointExplorer的样子,它实际上是ServerExplorer的一个插件





2.好吧,我们如何开始来做扩展呢?

先想清楚吧,我们希望在ContentType上面有一个快捷菜单,然后点击之后,可以查看到它的属性吧。应该还是比较简单的

下面这个链接中有比较清楚的介绍,我今天基本上是依葫芦画瓢了

http://msdn.microsoft.com/en-us/library/ee471438(VS.100).aspx

大致的步骤如下,我做了一个简单的翻译

ToextendaSharePointnodeinServerExplorerCreateaclasslibraryproject.(创建一个类库项目)

Addreferencestothefollowingassemblies:(添加如下的引用)Microsoft.VisualStudio.SharePoint

Microsoft.VisualStudio.SharePoint.Explorer.Extensions

System.ComponentModel.Composition

CreateaclassthatimplementstheIExplorerNodeTypeExtensioninterface.(创建一个类,继承一个接口)

AddtheSystem.ComponentModel.Composition.ExportAttributeattributetotheclass.ThisattributeenablesVisualStudiotodiscoverandloadyourIExplorerNodeTypeExtensionimplementation.PasstheIExplorerNodeTypeExtensiontypetotheattributeconstructor.(添加一个Attribute)

AddtheExplorerNodeTypeAttributeattributetotheclass.Thisattributespecifiesthestringidentifierforthetypeofnodethatyouwanttoextend.Tospecifybuilt-innodetypesprovidedbyVisualStudio,passoneofthefollowingenumerationvaluestotheattributeconstructor:ExplorerNodeTypes:Usethesevaluestospecifysiteconnectionnodes(thenodesthatdisplaysiteURLs),sitenodes,orallotherparentnodesinServerExplorer.

ExtensionNodeTypes:Usethesevaluestospecifyoneofthebuilt-innodesthatrepresentanindividualcomponentonaSharePointsite,suchasanodethatrepresentsalist,field,orcontenttype.(继续添加Attribute)

InyourimplementationoftheIExplorerNodeTypeExtension.Initializemethod,usemembersofthenodeTypeparametertoaddfeaturestothenode.ThisparameterisanIExplorerNodeTypeobjectthatprovidesaccesstotheeventsdefinedintheIExplorerNodeEventsinterface.Forexample,youcanhandlethefollowingevents:IExplorerNodeEvents.NodeChildrenRequested:Handlethiseventtoaddnewchildnodestothenode.Formoreinformation,seeHowto:AddaCustomSharePointNodetoServerExplorer.

IExplorerNodeEvents.NodeMenuItemsRequested:Handlethiseventtoaddacustomshortcutmenuitemtothenode.

IExplorerNodeEvents.NodePropertiesRequested:Handlethiseventtoaddcustompropertiestothenode.ThepropertiesappearinthePropertieswindowwhenthenodeisselected.

那我们还等什么呢?

将默认的类型名称修改为ContentTypeNodeExtension

【注意】需要确认框架版本为.NETFramework4.0

添加引用





3.修改一些简单代码
usingSystem;
usingMicrosoft.VisualStudio.SharePoint.Explorer;
usingMicrosoft.VisualStudio.SharePoint.Explorer.Extensions;
usingSystem.ComponentModel.Composition;
usingMicrosoft.VisualStudio.SharePoint;
usingSystem.Windows.Forms;

namespaceXizhang.ServerExplorerExtension
{

[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExtensionNodeTypes.ContentTypeNode)]
publicclassContentTypeNodeExtension:IExplorerNodeTypeExtension
{
publicvoidInitialize(IExplorerNodeTypenodeType)
{
System.IO.File.AppendAllText("c:\\log.txt",DateTime.Now.ToString());

nodeType.NodeMenuItemsRequested+=newEventHandler<ExplorerNodeMenuItemsRequestedEventArgs>(nodeType_NodeMenuItemsRequested);

}

voidnodeType_NodeMenuItemsRequested(objectsender,ExplorerNodeMenuItemsRequestedEventArgse)
{
IMenuItemmenuItem=e.MenuItems.Add("我的菜单");

menuItem.Click+=newEventHandler<MenuItemEventArgs>(menuItem_Click);
}

voidmenuItem_Click(objectsender,MenuItemEventArgse)
{

IExplorerNodenode=(IExplorerNode)e.Owner;

MessageBox.Show(string.Format("你点击了'{0}'节点,类型是'{1}'.",node.Text,node.NodeType.Name));

}
}
}

.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}



4.部署该扩展
详细的步骤可以参考http://msdn.microsoft.com/en-us/library/ee513825.aspx
更加详细的一个介绍http://msdn.microsoft.com/en-us/library/dd393694(v=VS.100).aspx
简单来说,我们需要创建一个所谓VSIX的项目来定义部署的信息,不过在此之前,你需要安装VS2010SDK
http://www.microsoft.com/downloads/details.aspx?familyid=4659F71D-4E58-4DCD-B755-127539E21147&displaylang=en


















.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

切换到全屏幕



点击“AddContent”



设置好之后,就可以编译该项目了。(以前我们要做一个VS的扩展包可远远没这么方便)
------RebuildAllstarted:Project:Xizhang.ServerExplorerExtension,Configuration:DebugAnyCPU------
Xizhang.ServerExplorerExtension->C:\Users\Administrator\Documents\VisualStudio2010\Projects\OrderListSolution\Xizhang.ServerExplorerExtension\bin\Debug\Xizhang.ServerExplorerExtension.dll
------RebuildAllstarted:Project:Xizhang.ServerExplorer.Extension.Package,Configuration:DebugAnyCPU------
Xizhang.ServerExplorer.Extension.Package->
Xizhang.ServerExplorer.Extension.Package->
Xizhang.ServerExplorer.Extension.Package->C:\Users\Administrator\Documents\VisualStudio2010\Projects\OrderListSolution\Xizhang.ServerExplorer.Extension.Package\bin\Debug\Xizhang.ServerExplorer.Extension.Package.dll
CreatingintermediatePkgDeffile.
CreatingVSIXContainer...
Xizhang.ServerExplorer.Extension.Package->C:\Users\Administrator\Documents\VisualStudio2010\Projects\OrderListSolution\Xizhang.ServerExplorer.Extension.Package\bin\Debug\Xizhang.ServerExplorer.Extension.Package.vsix
SettingupVisualStudiofordebuggingextensions.Thisone-timeoperationmaytakeaminuteormore.
==========RebuildAll:2succeeded,0failed,0skipped==========
【注意】它生成了一个vsix文件



5.安装该扩展
双击vsix文件






6.使用该扩展。重新打开VisualStudio,可以通过ExtensionManager看到我们安装好的扩展包






激动人心的时刻到了



我们可以点击“我的菜单”



到这里为止,我们这个扩展程序就实现了,如果你有兴趣,请将它修改得更加实用些吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐