SharePoint Study Notes-ecmascript object model series-12. Send a document link with a unique discovery entid by email

Source: Internet
Author: User

In the default ribbon of SharePoint document list, there is such a send email button, through which you can share the selected document to other users, such:

 

However, in the sent email, the default email button uses the URL address of the document to send, instead of the unique document ID provided by Sharepoint, the most obvious disadvantage of sharing a document with a document URL is that once we move this document, this URL becomes invalid, users who have shared this document will not be able to obtain this document through this URL. So here, we use ecmascript to combine the knowledge of ribbon to share the document with other users through the unique document ID provided by pivopoint2010. The procedure is as follows.

1. Create an empty SharePoint Project


2. Add a new feature and name it emaillinkfeature, as shown in figure

3. Add a new empty element named emaillinkelement.

TheCodeAs follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Elements xmlns = "http://schemas.microsoft.com/sharepoint/">
<Customaction
Id = "ribbon. emailuniquelink"
Location = "commandui. Ribbon"
Registrationid = "101"
Registrationtype = "list">
<Commanduiextension>
<Commanduidefinitions>
<Commanduidefinition
Location = "ribbon. Documents. Share. Controls. _ Children">
<Button
Id = "ribbon. Documents. Share. emailuniquelink"
Command = "ribbon. Documents. Share. emailuniquelink"
Sequence = "15"
Image16by16 = "/_ layouts/$ resources: Core, language;/images/formatmap16x16.png"
Image16by16top = "-16"
Image16by16left = "-88"
Image32by32 = "/_ layouts/$ resources: Core, language;/images/formatmap32x32.png"
Image32by32top = "-128"
Image32by32left = "-448"
Description = "sends the unique link to the document by E-mail"
Labeltext = "E-mail unique link"
Tooltiptitle = "E-mail unique link"
Tooltipdescription = "sends the unique link to the document by E-mail"
Templatealias = "O1"/>
</Commanduidefinition>
</Commanduidefinitions>
<Commanduihandlers>
<Commanduihandler
Command = "ribbon. Documents. Share. emailuniquelink"
Commandaction = "javascript: emailuniquelink ();"
Enabledscript = "javascript: enableemailuniquelink ();"/>
</Commanduihandlers>
</Commanduiextension>
</Customaction>
<Customaction
Id = "ribbon. Documents. Share. emailuniquelink. script"
Location = "scriptlink"
Scriptsrc = "/_ layouts/emaillinkbutton. js"/>

</Elements>

 

4. Add the SharePoint layouts directory and add a new Javascript file: emaillinkbutton. js under this directory.

The content of emaillinkbutton. JS is as follows:

// This method will contain in most of the code needed to request the unique URL to the document
Function Emailuniquelink (){
// First get the context and Web
VaR CTX = sp. clientcontext. get_current ();
This . Web = CTX. get_web ();
// Get the current selected list, then load the list using the getbyid method of Web (spweb)
VaR Listid = sp. listoperation. selection. getselectedlist ();
VaR Sdlist = This . Web. get_lists (). getbyid (listid );
// Get the currently selected item of the list. This will return a dicustonary with an ID field
VaR Items = sp. listoperation. selection. getselecteditems (CTX );
VaR Mijnid = items [0];
// Request the list item from the server using the getitembyid method. This will load all properties.
// If needed, one cocould pre-request the fields to be loaded to preserve bandwidth.
This . Listitem = sdlist. getitembyid (mijnid. ID );
// Load the item in the context for batch operation.
CTX. Load ( This . Listitem );
// Execute the actual script on the server side. Specify delegates to handle the response.
Ctx.exe cutequeryasync (function. createdelegate ( This , This . Onquerysucceeded), function. createdelegate ( This , This . Onqueryfailed ));
}

// Delegate that is called when server operation is complete upon success.
Function Onquerysucceeded (sender, argS ){
// Request URL by using the get_item method. It will return the URL field type, which has a URL property.
VaR Url = This . Listitem. get_item ('_ dlc_docidurl'). get_url ();
// Request the name of the document.
VaR Title = This . Listitem. get_item ('fileleafref ');
// Open a new E-mail in the default mail program.
Window. Open ('mailto :? Subject = emailing % 3A % 20' + title + '& Body =' + URL );
}

//Delegate that is called when server operation is completed with errors.
FunctionOnqueryfailed (sender, argS ){
Alert ('failed' + args. tostring ());
}

// Method To enable/disable the e-mail unique button on the ribbon.
Function Enableemailuniquelink (){
// Request number of selected items.
VaR Items = sp. listoperation. selection. getselecteditems ();
VaR Count = countdictionary (items );
// Only return true is a single item is selected.
Return (COUNT = 1 );
}

5. build and deploy our projects.
6. The test items are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.