moopoo.net
Fishing, technology and anything else

Disabling a SharePoint ribbon control based on user list permissions

December 9th, 2011 by Matt

I had a requirement to create a button on a SharePoint list ribbon that was only enabled for Contributors of that list. The post will explain how to set that up in javascript using the SharePoint 2010 Client Object Model.

I assume that you already have knowledge of working with the Ribbon in SharePoint 2010 and in particular using javascript page components. The snippets assume you have the rest of the ribbon definition in place and an existing page component file. If you don’t, I’d recommend you head on over to Chris O’Brien’s great articles on ribbon customisations.

Creating the button.

First up we need to declare the button we wish to disable or rather enable based on the users permission.

<Button
  Id="Ribbon.MyButton.Button"
  Alt="Ribbon.MyButton.Button"
  Command="Ribbon.MyButton.Button.Configure"
  Image16by16="/_layouts/images/splogo.gif"
  Image32by32="/_layouts/images/settingsIcon.png"
  LabelText="Configure"
  Sequence="10"
  TemplateAlias="o1"
  ToolTipTitle="Configure"
  ToolTipDescription="Configure merge" />

 

The above definition is just that of an ordinary ribbon button, nothing special is declared in there.

Javascript page component.

The next step is to modify our page component file.

canHandleCommand: function (commandId) {
      if(commandId === 'Ribbon.MyButton.Button.Configure')
        {
            if(userIsContrib == null)
            {
                checkUserIsContrib();                 
                return false;
            }
            else
            {
                return userIsContrib;
            }
        }
        else {
            return false;
        }
    },
...

var userIsContrib = null;

function checkUserIsContrib() {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    list = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());

    context.load(list,'EffectiveBasePermissions');

    context.executeQueryAsync(Function.createDelegate(this, gotUserIsContrib), Function.createDelegate(this, failedUserIsContrib));
}

function gotUserIsContrib() {

    // update the variable based on the users permission
    userIsContrib = list.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems);

    //Update the UI
    RefreshCommandUI();

}

function failedUserIsContrib() {
    alert("failed failedUserIsContrib");
}

 

Because we want to check the user permissions asynchronously, we need to set userIsContrib initially to null. This will disable the button for all users while the checkUserIsContrib() method goes off and checks the users permissions.

After checking the users permissions and settings the flag appropriately we call RefreshCommandUI() which will again cause canHandleCommand to be run again. This time it will return the value that was set in the gotUserIsContrib() method. As a value has now been set on userIsContrib, no more checks against the server will be made by any other calls to RefreshCommandUI.

 

References.

Posted in SharePoint | No Comments »

Programmatically checking if a user belongs to an SharePoint Audience

December 9th, 2011 by Matt

The following code will check if a user belongs to a given Audience in SharePoint

using (SPSite elevatedSite = new SPSite(Microsoft.SharePoint.SPContext.Current.Site.ID))
{
 
    SPServiceContext serviceContext = SPServiceContext.GetContext(elevatedSite);
    AudienceManager audManager = new AudienceManager(serviceContext);
    Audience audience = audManager.GetAudience("AudienceToCheck");
 
    SPUser user = SPControl.GetContextWeb(Context).CurrentUser;
 
    if (audience.IsMember(user.Name))
    {
          // do some stuff
     }
}

Posted in SharePoint | No Comments »

Package class library as wsp for SharePoint Deployment

December 15th, 2010 by Matt

Some Visual Studio solutions I’ve come across have class library projects in them. These assemblies have to subsequently be manually copied into the GAC of each SharePoint server. This goes against the standard of using wsp files to deploy assemblies and files into a SharePoint environment. Single servers are easier to manage manually copying assemblies but it’s not a very pleasant way of doing things, move into a multi server environment and you’re in a minefield.
Fortunately there is an easy way to get these assemblies, plus any others you may wish to deploy at the same time, into a wsp. It takes two files and a post build event. Read on..

First up, we need a manifest file. This tells the environment what we would like it to do with the assembly.
Create a new xml file in the root of your project called manifest.xml and add the following code:

<?xml version="1.0" encoding="utf-8" ?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/"
  SolutionId="<new guid>">
  <Assemblies>
    <Assembly Location="<assembly>.dll" DeploymentTarget="GlobalAssemblyCache"></Assembly>
  </Assemblies>
</Solution>

Replace <new guid> with a generated GUID and <assembly> with the name of your dll file.
The DeploymentTarget attribute above specifies that it will be deployed to the GAC, you can use WebApplication if you want it to be deployed to the bin directory of your Webb Application.

You can also specify that the relevant line can be added to the SafeControls section of your web.config file by adding the following inside the Assembly tag above.

<SafeControls>
        <SafeControl Assembly="<assembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<public key token>" Namespace="<namespace>" TypeName="*" Safe="True" />
      </SafeControls>

Next up, create a ddf file in the root of your project. I tend to name mine wsp.ddf for consistency. This will contain the instructions on what to package into the wsp file. This file should look like the following.

;Define the output directory and CAB file name (with a wsp extension)
.Set DiskDirectory1="bin\Debug"
.Set CabinetNameTemplate="MySolutionName.wsp"
 
;Include the following files in the CAB Root
manifest.xml
bin\Debug\<assembly>.dll

Now all we have left to do is add a post build event to build the wsp for us.
Go to you project properties (right click project)
Under the Build Events tab under Post-build event command line, enter the following:

:: Change directory to the root of the project
cd "$(ProjectDir)"
 
:: Create a WSP CAB
MakeCAB /f "WSP.DDF"

You can change when the post build event runs, to your liking; normally ‘On successful build’.

That’s it when you have a successful build the wsp file will be generated ready for you to deploy, no more manual copying or editing web.config.

Posted in SharePoint | 1 Comment »

Arrays in custom web part and SharePoint Designer

September 17th, 2010 by Matt

I came across an issue when trying to add a custom web part to a page layout in SPD.
Each time I added it, the web part would error “Cannot create an object of type ‘Systems.Collections.ArrayList’ from its string representation ‘Collection’ for the ‘MyValue’ property”.
I narrowed it down to an ArrayList I had declared as public within the webpart. Changing this from public solved the issue for me.

Posted in SharePoint | No Comments »

Required Field Validation Group on PeopleEditor control

June 9th, 2010 by Matt

Very similar to my post on validation of the DateTimeControl, a PeopleEditor (people picker) control I was using was validating on each postback.
I created a required field validator with a ValidationGroup and to get the validator to look at the correct people editor field use the following code.

myRequiredFieldValidator.ControlToValidate = string.Format("{0}$downlevelTextBox", myPeopleEditor.ID);

Posted in SharePoint | 1 Comment »

Removing element from web.config on feature de-activation

March 15th, 2010 by Matt
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
 
            SPSite site = (SPSite)properties.Feature.Parent;
 
            SPWebConfigModification controlsSection = new SPWebConfigModification("controls", "configuration/system.web/pages");
            controlsSection.Owner = "My.Menu";
            controlsSection.Sequence = 20;
            controlsSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection;
            controlsSection.Value = "<controls></controls>";
 
            SPWebConfigModification addElement = new SPWebConfigModification(
                @"add[@tagPrefix=""SharePoint""][@namespace=""My.Menu""][@assembly=""My.Menu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd3368f0ff736bff""]", 
                "configuration/system.web/pages/controls");
            addElement.Owner = "My.Menu";
            addElement.Sequence = 40;
            addElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            addElement.Value = @"<add tagPrefix=""SharePoint"" namespace=""My.Menu"" assembly=""My.Menu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd3368f0ff736bff"" />";
 
            site.WebApplication.WebConfigModifications.Remove(controlsSection);
            site.WebApplication.WebConfigModifications.Remove(addElement);
            site.WebApplication.Update();
            site.WebApplication.WebService.ApplyWebConfigModifications();
 
    }

Posted in SharePoint | No Comments »

Adding element to web.config on feature activation

March 15th, 2010 by Matt
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
 
            SPSite site = (SPSite)properties.Feature.Parent;
 
            SPWebConfigModification controlsSection = new SPWebConfigModification("controls", "configuration/system.web/pages");
            controlsSection.Owner = "My.Menu";
            controlsSection.Sequence = 20;
            controlsSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection;
            controlsSection.Value = "<controls></controls>";
 
            SPWebConfigModification addElement = new SPWebConfigModification(
                @"add[@tagPrefix=""SharePoint""][@namespace=""My.Menu""][@assembly=""My.Menu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd3368f0ff736bff""]", 
                "configuration/system.web/pages/controls");
            addElement.Owner = "My.Menu";
            addElement.Sequence = 40;
            addElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            addElement.Value = @"<add tagPrefix=""SharePoint"" namespace=""My.Menu"" assembly=""My.Menu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd3368f0ff736bff"" />";
 
            site.WebApplication.WebConfigModifications.Add(controlsSection);
            site.WebApplication.WebConfigModifications.Add(addElement);
            site.WebApplication.Update();
            site.WebApplication.WebService.ApplyWebConfigModifications();
 
    }

Posted in SharePoint | No Comments »

Securing custom application pages in _layouts directory

February 26th, 2010 by Matt

I had a need to be able to only allow site collection admins to see some custom pages I had deployed into the _layouts directory.
Create a custom code behind class for your aspx that inherits from LayoutsPageBase.
Next, override the OnLoad method in your code behind and add the following code

if (!SPContext.Current.Web.UserIsSiteAdmin)
              SPUtility.HandleAccessDenied(new Exception("You need to be a site administrator to access this page."));

This will redirect the user to the built in SharePoint Access denied page, for consistency.

Posted in SharePoint | No Comments »

Validation Group with SharePoint DateTimeControl

February 16th, 2010 by Matt

The SharePoint DateTimeControl has a property called IsRequiredField, which will act like a RequiredFieldValidator for the control. The problem is when working with web parts in SharePoint calling Page.Validate in your web part can have unexpected results. It often ends up firing validators elsewhere on the page, particularly if the web part is used on a list form for example.
One answer of course is to use a validation group in your web part, this is where the DateTimeControl falls down as it doesn’t have this property.

The answer to this to create a validator in your webpart and hook it into the TextBox element of the DateTimeControl, which will then allow you to set the validation group on it.

To do this use the following code when you create your validator.

valReviewDate.ControlToValidate = string.Format("{0}${0}Date", dteReview.ID);

This post, describes how to do it from code in front.

Posted in SharePoint | No Comments »

Disable Web Part button after clicking

February 16th, 2010 by Matt

Not sure how, I got to the end point of this so there may be other ways of doing it.
Basically I had a web part the when the button was clicked I needed to disable it.
Adding .Attributes["onclick"] didn’t seem to work, and after trying a few things I managed to get it working this way.
Add the following code into OnPreRender of the web part, assuming cmdGenerate is the name of your button.

cmdGenerate.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(cmdGenerate, "") + ";this.value='Please wait...';this.disabled = true;");

Posted in SharePoint | No Comments »

« Previous Entries