SharePoint learning notes-ecmascript object model series-11, enable/disable ribbon button

Source: Internet
Author: User

Here we want to achieve the following goals:
1. Create a button control at the ribbon. Library. viewformat position of ribbon.
2. Determine whether the user is entitled to use the (enable) button based on whether the current user is in a specific groups.
3. The function of this button is to jump out of a simple information prompt box.

The effect is as follows:

 

The button works as follows:

 

The procedure is as follows:
1. Create a new project named Farm solution
2. Add a new feature to this project.
3. Add a new "Empty element" to this project"
4. Definition of this elementCodeAs follows:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Elements Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Customaction
ID = "Buttonforgroupusersonly"
Location = "Commandui. Ribbon"
Registrationid = "101"
Registrationtype = "List"
Title = "Owners group button" >
< Commanduiextension >
< Commanduidefinitions >
< Commanduidefinition
Location = "Ribbon. Library. viewformat. Controls. _ Children" >
< Button ID = "Ribbon. Library. viewformat. usersbtn"
Command = "Usersbtncommand"
Labeltext = "Group users button"
Templatealias = "O1"   /> "
</ Commanduidefinition >
</ Commanduidefinitions >
< Commanduihandlers >
< Commanduihandler
Command = "Usersbtncommand"
Commandaction = "Javascript: Alert ('Action from this button! ');"
Enabledscript = "Javascript: enablerscript (4, 5 );" />
</ Commanduihandlers >
</ Commanduiextension >
</ Customaction >
< Customaction
ID = "Ribbon. Library. viewformat. scripts"
Location = "Scriptlink"
Scriptsrc = "/_ Layouts/spriboontest/checkuseringroup. js" />

</Elements>

5. Add the javascrip file checkuseringroup. js with the following content:

// /<Reference name = "microsoftajax. js"/>
// /<Reference Path = "file: // C:/program files/common files/Microsoft shared/Web Server Extensions/14/template/layouts/SP. core. debug. JS "/>
// /<Reference Path = "file: // C:/program files/common files/Microsoft shared/Web Server Extensions/14/template/layouts/sp. Debug. js"/>

VaRIdgrouptoverify1 = 4;
VaRIdgrouptoverify2 = 5;
VaRNamegroup1 = "devptest owners ";
VaRNamegroup2 = "devptest visitors ";
VaRArrisinthisgroup =NewArray (2 );
Arrisinthisgroup ["devptest owners"] =False;
Arrisinthisgroup ["devptest visitors"] =False;

Executeordelayuntilscriptloaded (initgroups, "sp. js ");

// Initialize groups IDs for the current user
Function Initgroups (){
// Debugger;
Checkuser (idgrouptoverify1, namegroup1 ); // Judge if user is in the given group
// Beacuse of some errors if execute together
SetTimeout ("checkuser (" + idgrouptoverify2 + "," + namegroup2 + "')", 500 );
}

// Groupids is all the groups we need to check, but only 4 and 5 can enable the button
Function Enablerscript (groupids ){
If (Groupids ){
VaR Isbuttonenabled = False ;
// Groupid comes in string comma separated
// '4' or '4, 5'
VaR Arrgrid = groupid. Split (',');
VaR I = 0;
For (I = 0; I <arrgrid. length; I ++ ){
VaR Currgroupid = arrgrid [I];
If (Currgroupid = idgrouptoverify1)
Isbuttonenabled = isbuttonenabled | arrisinthisgroup [namegroup1];
If (Currgroupid = idgrouptoverify2)
Isbuttonenabled = isbuttonenabled | arrisinthisgroup [namegroup2];
}
Return Isbuttonenabled;
}
Return False ;
}

// The below checks if the user exists in the group
Function Checkuser (groupid, isinthisgroup ){
// Debugger;
VaR Context = sp. clientcontext. get_current ();
// I go to parent site if I'm in a subsite!
VaR Sitecoll = context. get_site ();
Web = sitecoll. get_rootweb ();
VaR Groupcollection = web. get_sitegroups ();

// Get the our group's ID
VaR _ Group = groupcollection. getbyid (groupid ); // ID of the group that we are checking
VaR Users = _ group. get_users (); // Get all users of the group we are checking
Context. Load (_ group );
Context. Load (users );
This . _ Users = users; // Get users of the checking Group
This . _ Currentuser = web. get_currentuser (); // Get current Login User
This . _ Isinthisgroup = isinthisgroup;
Context. Load ( This . _ Currentuser );
Context.exe cutequeryasync (function. createdelegate ( This , This . Checkusersucceeded), function. createdelegate ( This , This . Failed ));
}

// The below checks if user is the member of the specified group
Function Checkusersucceeded (){
// Debugger;
If ( This . _ Users. get_count ()> 0 ){
VaR _ Usersenum = This . _ Users. getenumerator ();
While (_ Usersenum. movenext ()){ // Scan all the users in specific group and to see if the current user exists in these users
VaR User = _ usersenum. get_current (); // Get current Login User
If (User. get_loginname () = This . _ Currentuser. get_loginname ()){ // Compare user
// Debugger;
Arrisinthisgroup [ This . _ Isinthisgroup] = True ; // If the user exists in the group then set the flag to be true
}
}
}
}

FunctionFailed (sender, argS) {alert ("error ");}

The project is as follows:

 

Note:
1. For more information about how to allow a user-defined ribbon to reference external JavaScript files, see SharePoint Study Notes-ribbon series-9. How to Make a user-defined ribbon reference external JavaScript files.
2. In checkuseringroup. in JS, we predefine the groups that can enable this button (the groupid is 4 and 5 respectively). If the goups to be checked is not in this predefined goups, or if the current loginuser is not in this predefined goups, this button is unavailable. The checkuseringroup. js code is as follows. You may modify it based on your own situation.

3. If the enablescript attribute in <commanduihandler> is true, the button is enable; otherwise, the disable value is set through the Javascript {javascript: enablescript )} return Value.

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.