在这个document library中的document的dropdown menu下多了个”Save as PDF”.我们在真实项目中经常也会碰到要做一些特殊menu的需求。下面就简单的介绍一下这个dropdown menu是怎么做出来的。
A, 新建一个SharePoint 空项目。
B, 在Feature地方加一个新的feature。
C, 在当前项目中加一个element.
在Element加入CustomAction部分。
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="{F2ACC1B0-0D93-4E45-B1AF-ED13903CF8CA}"
RegistrationId="101"
RegistrationType="List"
Location="EditControlBlock"
Title="Save as PDF"
GroupId="ActionsMenu"
Sequence="100" ImageUrl="/_layouts/images/DOC16.gif">
<UrlAction Url="{SiteUrl}/_layouts/ConvertToPDF/ConvertToPDF.aspx?List={ListId}&ID={ItemId}"/>
</CustomAction>
</Elements>到这里,这个Action Menu就已经做好了,Deploy时就会出现前面提到的画面。但是如果我们想做事情,就要在它跳转后的页面里做啦。下面我简单的说下Convert to PDF.
D, 现在在project下加一个layouts,在它下面就可以直接添加web page了。
web page的URL: <UrlAction Url="{SiteUrl}/_layouts/ConvertToPDF/ConvertToPDF.aspx?List={ListId}&ID={ItemId}"/>
E, 现在开始在ConvertToPDF这个web page里实现所需功能。
由于要使用Office API中自带的convert doc/docx to PDF的功能,所以要引入它的DLL。
F, 功能的实现。
public partial class ConvertToPDF : LayoutsPageBase//System.Web.UI.Page
{
SPSite siteCollection = null;
Guid listId = Guid.Empty;
int itemId = 0;
protected void Page_Load(object sender, EventArgs e)
{
try
{
siteCollection = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
listId = new Guid(Server.UrlDecode(Request.QueryString["List"]));
itemId = int.Parse(Request.QueryString["ID"]);
SPDocumentLibrary docLibrary = web.Lists[listId] as SPDocumentLibrary;
SPListItem item = docLibrary.GetItemById(itemId);
if (item.Name.EndsWith(".doc") || item.Name.EndsWith(".docx"))
{
string new_name = "";
if (item.Name.EndsWith(".docx"))
{
new_name = item.Name.Replace(".docx", ".pdf");
}
else
{
new_name = item.Name.Replace(".doc", ".pdf");
}
//docx file
string file = web.Url + "/" + item.Url;
//pdf file
string new_file = file.Replace(".docx", ".pdf");
//schedule the conversion
string wordAutomationServiceName = "Word Automation Services";
Word.Conversions.ConversionJob conversionJob = new Word.Conversions.ConversionJob(wordAutomationServiceName);
conversionJob.Name = "Proposal Conversion";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
web.AllowUnsafeUpdates = true;
conversionJob.UserToken = siteCollection.UserToken;
conversionJob.AddFile(file, new_file);
conversionJob.Start();
web.AllowUnsafeUpdates = false;
});
/*ConversionJobStatus status = new ConversionJobStatus(wordAutomationServiceName, conversionJob.JobId, null);
bool isSuccess = false;
while (true)
{
Thread.Sleep(5000);
status = new ConversionJobStatus(wordAutomationServiceName, conversionJob.JobId, null);
if (status.Count == status.Succeeded)
{
isSuccess = true;
break;
}
else if (status.Count == status.Failed)
{
break;
}
}
if (isSuccess)
{*/
//}
lblMessage.Visible = false;
Response.Write("<script language='javascript'>history.go(-1);</script>");
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "The action can not support the current format.";
}
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>history.go(-1);</script>");
}
}
private void Download(string fileName,byte [] content)
{
Response.ClearHeaders();
Response.Clear();
Response.Expires = 0;
Response.Buffer = true;
Response.AddHeader("Accept-Language","en-us");
Response.AddHeader("Content-Disposition", "Attachment;FileName=" + System.Web.HttpUtility.UrlEncode(fileName,System.Text
.Encoding.UTF8));
Response.ContentType = "APPLICATION/octet-stream";
Response.BinaryWrite(content);
Response.Close();
}
}
----------------------aspx
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label ID="lblMessage" runat="server" />
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Save as PDF
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Save as PDF
</asp:Content>
这里起的是一个TimeJob,如果发现要等很久才能生成PDF,我们可以把这个Time job的周期变短。
------------------------------------------------------------------------------------------------------------------------------------------
Custom Ribbon button for Document library/SPList.
由于自定义Ribbon button和自定义Item action menu基本一样我就不详细写了,我说一下他们的区别。
主要是Element内容不同。
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="Ribbon.ListItem.Actions.BuildCustomerDocuments"
Location="CommandUI.Ribbon"
RegistrationId="105"
RegistrationType="List"
Title="Build Customer Document">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.ListItem.Actions.Controls._children">
<Button Id="Ribbon.ListItem.Actions.BuildCustomerDocumentsButton"
Image16by16="/_layouts/images/DOC16.gif"
Image32by32="/_layouts/images/DOC32.gif"
LabelText="Build Customer Doc"
Sequence="100"
TemplateAlias="o1"
Command="HelloCommand" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="HelloCommand"
CommandAction="javascript:GoToPage('{SiteUrl}/_layouts/CustomerDocumentsFeature/BuildCustomerDoc.aspx?List={ListId}&ID={SelectedItemId}');"/>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>