In order to work with SharPoint's
User Profile Service Application beyond a read-only capacity, a user account must be granted appropriate access. Otherwise, you'll encounter errors such as
ActivityFeedPermissionDeniedException when attempting to perform operations such as
ActivityEvent.CreateActivityEvent.
The following method will grant access to User Profile Service Application for a specified account name of the format DOMAIN\User.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private static void GrantPermissionsToUserProfileService(string accountName) { var upServiceproxy = SPFarm.Local.Services.Where(s => s.GetType().Name.Contains("UserProfileService")).FirstOrDefault(); if (upServiceproxy != null) { var upServiceApp = upServiceproxy.Applications.OfType<SPIisWebServiceApplication>().FirstOrDefault(); if (upServiceApp != null) { var mgr = SPClaimProviderManager.Local;
var security = upServiceApp.GetAccessControl(); var claim = mgr.ConvertIdentifierToClaim(accountName, SPIdentifierTypes.WindowsSamAccountName); security.AddAccessRule(new SPAclAccessRule<SPIisWebServiceApplicationRights>(claim, SPIisWebServiceApplicationRights.FullControl)); upServiceApp.SetAccessControl(security);
var adminSecurity = upServiceApp.GetAdministrationAccessControl(); var adminClaim = mgr.ConvertIdentifierToClaim(accountName, SPIdentifierTypes.WindowsSamAccountName); adminSecurity.AddAccessRule(new SPAclAccessRule<SPCentralAdministrationRights>(adminClaim, SPCentralAdministrationRights.FullControl)); upServiceApp.SetAdministrationAccessControl(adminSecurity);
upServiceApp.Uncache(); upServiceproxy.Uncache(); } } }
|
5 comments:
Thanks for sharing.
Thanks for sharing.
This information is very helpful.
Thank you!
Thanks for sharing, but I have a problem when I'm trying to set the access control, I get "Access denied" message even if I use Administrator account. Can somebody help me with that issue?
Hi, Great post!!!
Carlos, I have also encountered with the same access denied issue while setting the access rule. Actually I was using this code in list event receiver class under ItemAdding event. This error was expacted as this code will run under the normal user credential and will use the server context based on the logged on user (not by farm administrator).
So finally I thought a bit then created a timer job which will run after a duration and will execute this code with admin privileges and finally got success :).
Hope its not too late for you to implement this approach.
Thanks
Mohit Vashishtha
http://mohitvash.wordpress.com
Post a Comment