开发者

Is it possible to programmatically determine the type of a SharePoint object?

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.Ad开发者_C百科d(roleAssignment);

(code snippet from Link)

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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜