是否可以以编程方式确定SharePoint对象的类型?

时间:2010-06-14 14:03:57

标签: c# sharepoint sharepoint-2007 oop

我正在使用SharePoint 2007对象模型API将角色分配给SharePoint对象,但是当您执行此操作时,您必须事先知道SharePoint对象的类型,如下所示:

// 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);

(来自http://blogs.msdn.com/b/robgruen/archive/2007/11/15/how-to-programmatically-set-permissions-on-files-folders-in-a-sharepoint-document-library.aspx的代码段)

我想要实现一个能够传递SharePoint对象的方法,确定SharePoint对象的类型如下所示:

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

示例SharePoint对象类型将是:SPSite,SPWeb,SPList,SPListItem,SPField,SPFolder

非常感谢有关如何解决确定SharePoint对象类型问题的任何帮助。

旁注:
There is a way to determine the object type(有点),如果你知道对象在网站中的位置的完整网址,虽然这不是我想要去的路线。

1 个答案:

答案 0 :(得分:3)

我认为你不想重复代码?

而不是对象为什么不使用SPRoleAssignmentCollection

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

您将该功能称为

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

而不是您想要的

AssignRole(spWebUrl, spObject, oUser, spRoleDefinition); 

如果你真的想传递一个对象,你至少有以下选项

  • 使用is关键字
  • 使用GetType和TypeOf方法来比较类型标识。

有关上述两种用法的示例以及它们之间的区别,请参阅:http://bytes.com/topic/c-sharp/answers/641093-difference-between-gettype-typeof-class

我不建议有这样的方法。

什么阻止方法的用户传入List? 现在,您需要一个在无效对象中传递时抛出的异常(或返回值)。

此外,如果sharepoint添加可以分配角色的新对象,会发生什么?如果您已将此代码发送给许多客户,您现在是否会发布更新?

希望有所帮助。祝你好运!

相关问题