Question

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

Was it helpful?

Solution

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);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top