1

I have been trying for some time now to do something that should be simple using JAVASCRIPT CSOM in SharePoint 2010. I have working code that can break list item permission and add a user to an item with a role. All I am trying to do is instead of adding a user, add an existing group. I have a handle to the group. The code executes without error, but it does not actually update the list item permissions on the item.

I have followed several of the MSDN articles but these are user based and the one group example is for a list and is creating the group which I am not doing.

Any examples of doing this would be appreciated.

0

Apparently Context Matters!!

No, I had made a blunder in my code that I did not notice before. I was trying to update items on a parent site from a subsite. Not sure why, but I moved all my code to the parent site and it worked as expected! Lesson Learned! Will have to add this to my blog as it is really cool!!

SP.ClientContext.get_current() is different than SP.ClientContext(siteurl) even if the siteurl is pointing to the correct site. Something to keep in mind!!

0

The following code adds a group, it can be an Active Directory Group or a Sharepoint Group, to line item permition level using only javascript.

    var groupName = L_Menu_BaseUrl == "" ? 'domain\\all' : 'domain\\managing'; //group name

    function breakSecurityInheritanceChangeUser(id, groupName) {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('ListName');

        var itemId = id;
        this.oListItem = oList.getItemById(itemId);
        oListItem.breakRoleInheritance(true);
        this.oUser = clientContext.get_web().ensureUser(groupName);


        var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

        collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator)); //full control

        oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);
        clientContext.load(oUser);
        clientContext.load(oListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, function () { Success(groupName); }), Function.createDelegate(this, this.Failure));
    }

    function Success(groupName) {
        var text = $("#hiddenSecurityItemSuccess").val(); //hidden for logging sucess
        var temp = groupName;
        text =+ temp + "<br/>";
        $("#hiddenSecurityItemSuccess").val(text);

    }

    function Failure(sender, args) {
        var text = $("#hiddenSecurityItemError").val(); //hidden for logging error
        var temp = args.get_message();
        text =+ temp + "<br/>";
        $("#hiddenSecurityItemError").val(text);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.