views:

49

answers:

1

I'm using the SharePoint 2007 object model API to assign a role to a SharePoint object, however when you do this, you must know the type of SharePoint object beforehand, like this:

// apply the new roleassignment to the folder.  You can do this at the listitem level   if desired (i.e. this could be SPfile.Item…. instead of SPFolder.Item)
folder.Item.RoleAssignments.Add(roleAssignment);

(code snippet from http://blogs.msdn.com/b/robgruen/archive/2007/11/15/how-to-programmatically-set-permissions-on-files-folders-in-a-sharepoint-document-library.aspx)

I'd like the implementation of a method that is able to pass the SharePoint object, determine the type of SharePoint object to look like this:

public static bool AssignRole(string spWebUrl, object spObject, SPUser oUser, string spRoleDefinition);

Example SharePoint object types would be: SPSite, SPWeb, SPList, SPListItem, SPField, SPFolder

Any help on how to tackle the issue of determining the type of the SharePoint object would be much appreciated.

Sidenote:
There is a way to determine the object type (sort of), if you know the full url of where the object lies in the site, although that's really not a route I'd like to go down.

+3  A: 

I presume you don't want to duplicate code?

Instead of object why not use SPRoleAssignmentCollection ?

AssignRole(string spWebUrl, SPRoleAssignmentCollection spAssignmentCollection, 
           SPUser oUser, string spRoleDefinition); 

And you call the function as

AssignRole(spWebUrl, spObject.RoleAssignments, oUser, spRoleDefinition); 

instead of your intended

AssignRole(spWebUrl, spObject, oUser, spRoleDefinition); 

If you really really want to pass an object you have at least the following options

  • use the is keyword
  • use the GetType and TypeOf methods to compare for type identity.

For an example of usage of above two and also their difference see: http://bytes.com/topic/c-sharp/answers/641093-difference-between-gettype-typeof-class

I would not advise having such a method though.

What prevents users of the method from passing in a List ? Now you would need an exception which is thrown (or a return value) when passed in an invalid object.

Also, what happens if sharepoint adds new objects which can be assigned roles? If you have already shipped this code to many customers, would you now ship an update?

Hope that helps. Good luck!

Moron
I don't think that will help in my situation. I want to use one method to assign permissions to any sharepoint object. I want to pass the sharepoint object as a parameter, and then in the method determine the SharePoint type, and assign the permission appropriately. I'd prefer not to have a method to assign permissions to a folder, and a method to assign permissions to a list as well, etc. Maybe what I'm asking is not possible?
program247365
@program247365: It is possible using C# itself. I am not sure Sharepoint has something. I have edited my answer to add that info.
Moron
Moron's solution is good. It makes no sense to write a method that can handle any kind of SPObject because finally only objects inherited form SPSecurableObject can store permissions. SPField cannot store permissions at all and SPSite is a special case. SPSite administrators are set via the SPUser's property "IsSiteAdmin" and every other permission is set on the SPSite's root SPWeb. So Moron's solution can handle all the other objects you mentioned in you question.
Flo
@Flo Thank you for your comment, I see what you mean.@Moron, I appreciate your answer!
program247365