1

I am developing a sharepoint feature that should allow only Farm admin to delete a sitecollection. In SiteDeleting event, i need to chech if the user deleting is farmadmin. How should i do that? I got a property to check if user is webadmin(properties.Web.UserIsWebAdmin) or siteadmin(properties.Web.UserIsSiteAdmin) but how to check if the user is farm admin ?

Any help is much appreciated..

Bala
  • 13
  • 2

2 Answers2

1
SPFarm farm = SPFarm.Local;
farm.CurrentUserIsAdministrator();

These classes reside in Microsoft.SharePoint.Administration namespace. More on CurrentUserIsAdministrator or SPFarm class on MSDN.

Janis Veinbergs
  • 6,907
  • 5
  • 48
  • 78
  • I'd upvote if there were instructions on how to get the current farm here - this is a non-static method... – Code Jockey Oct 15 '13 at 14:29
  • @CodeJockey sure. Edited answer. By the way the second answer by user2086490 had what you were looking for :) – Janis Veinbergs Oct 16 '13 at 06:55
  • hmmmmm... well, not quite, as I'm not looking to reinvent the wheel - if there is an API function to use rather than defining a custom function with a delegate and a loop to call instead, then I would prefer the API function - unless there is a significant difference. Also - you were here the day before, while user2086490 hasn't been back since May 2 at 14:47. Granted, some people might really be able to use the loop or the function or be able to do something with the Farm Admin group itself or one or more of its members, and your answer doesn't provide those things, so... they're both useful – Code Jockey Oct 16 '13 at 16:54
0
public static bool IsFarmAdmin(string loginName)
            {
                //For Currently Logged in users  
                //SPFarm.Local.CurrentUserIsAdministrator();  

        bool isFarmAdmin = false;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPGroup adminGroup = SPAdministrationWebApplication.Local.Sites[0].AllWebs[0].SiteGroups["Farm Administrators"];

            foreach (SPUser user in adminGroup.Users)
            {
                if (user.LoginName == loginName)
                {
                    isFarmAdmin = true;
                }
            }

        });

        return isFarmAdmin;
    } 
Daniele Armanasco
  • 7,289
  • 9
  • 43
  • 55