1

By using feature stapling we activate a feature that creates a custom list on a users profile in my site web-application each time a new my site (site collection) is created for a user. We want to break inheritance on the list, and set current users manager as contributor on this list.

The list is created and the inheritance is broken, but the manager is not set as contributor. If I reactivate the feature after the first time automatic activation of the feature, then the manager is set.

This is my code for setting the manager as a contributor.

    private static void AddManagerToList(SPList list)
    {
        try
        {
            var currentWeb = SPContext.Current.Web;
            var currentUser = SPContext.Current.Web.CurrentUser;
            var serverContext = SPServiceContext.GetContext(currentWeb.Site);
            var profileManager = new UserProfileManager(serverContext);
            var currentUserprofile = profileManager.GetUserProfile(currentUser.LoginName);

            if (currentUserprofile != null)
            {
                var manager = currentUserprofile.GetManager();
                if (manager != null)
                {
                    list.BreakRoleInheritance(true);
                    var managerAccountLoginName = manager.AccountName;
                    var roleAssignment = new SPRoleAssignment(currentWeb.EnsureUser(managerAccountLoginName).ToString(), "user@dom", manager.DisplayName, "");
                    var roleDefinition = currentWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

                    list.RoleAssignments.Add(roleAssignment);
                }
            }
            currentWeb.Dispose();
        }
        catch (Exception ex)
        {
            LoggingService.LogError(Category.EventReceiver, "Exception in AddManagerToList " + ex.Message + ", " + ex.InnerException);
        }
    }

Can you guys see any error in this code? Any ideas of how to make it work on first time activation on provisioning of mysite ?

Thanks

  • 3
    It sounds like a timing problem in that the site isn't fully created yet when this executes. If it works by deactivating and activating, you know the code is fine. – Eric Alexander Feb 23 '15 at 17:31
  • Any idea how to avoid timing issue ? I have also tried to create a new feature where I shifted the logic to add the manager, and activate it programatically from the feature where the list is created. – Ilyas Feb 23 '15 at 17:38
  • 2
    To me it seems like this should be code as part of the provisioning process, or an event receiver that fires on creation of your custom list. A hacky work around would be to keep thread sleeping if the list doesn't exist. Once it does, break and call your method. – Eric Alexander Feb 23 '15 at 17:47
  • I found that I get exception when I try to retrieve manager of current user. Some flaws in the code there. Difficult to find, but will post when I find it. In the meantime, if anybody got some more tips, feel free to share :) – Ilyas Feb 23 '15 at 22:24
  • 3
    It seems like you would need to pass in a username or a user object to get the proper user context, when they physically activate the feature that information is available, but if you're doing it as part of the provisioning process, the current user would be the system account – Eric Alexander Feb 24 '15 at 12:57
1

The answer to this problem is solved by retrieving the site collection administrator.

 private static void AddManagerToList(SPSite site, string listTitle)
    {
        try
        {
            var web  = site.OpenWeb();                       
            var list = web.Lists.TryGetList(listTitle);

            if (list != null)
            {
                var siteOwner = web.Site.Owner.LoginName; //site.Owner.LoginName;                               
                var serverContext = SPServiceContext.GetContext(web.Site);
                var profileManager = new UserProfileManager(serverContext);
                var currentUserprofile = profileManager.GetUserProfile(siteOwner);

                var manager = currentUserprofile.GetManager();

                if (manager != null)
                {
                    var managerAccountLoginName = manager.AccountName;

                    var roleAssignment = new SPRoleAssignment(web.EnsureUser(managerAccountLoginName).ToString(), "user@dom", manager.DisplayName, "");
                    var roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                    list.RoleAssignments.Add(roleAssignment);
                }

            }
        }
        catch (Exception ex)
        {
            LoggingService.LogError(Category.EventReceiver, "Error activating feature : " + ex.Message + ", " + ex.InnerException);
        }
    }

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.