在C#做工作流时,总是出现分配权限错误。找了几天原因,终于找到问题所在:当文档继承了父权限时,是不可以随便更改权限的。所以在代码中添加了如下语句:
//
当前条目是父继承权限时
if ( ! item . HasUniqueRoleAssignments)
{
// 取消父继承权限并复制父权限
item . BreakRoleInheritance(true);
}
if ( ! item . HasUniqueRoleAssignments)
{
// 取消父继承权限并复制父权限
item . BreakRoleInheritance(true);
}
可当工作流同时对2个文档操作时,莫名的发生错误。甚是奇怪,找不到问题的原因,但问题的原因就是因为上面这句。所以我换了个方法去实现这句话,就用到了SharePoint的事件处理机制:Event handles.代码如下:
1、创建C#项目 -> 类库,取名 BreakRoleInheritance。
2、添加引用window#sharepoint#service
3、写入代码如下:
using
System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace BreakRoleInheritance
... {
public class BreakItemRoleInheritance : SPItemEventReceiver
...{
public override void ItemAdded(SPItemEventProperties properties)
...{
SPListItem item = properties.ListItem;
item.BreakRoleInheritance(true);
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace BreakRoleInheritance
... {
public class BreakItemRoleInheritance : SPItemEventReceiver
...{
public override void ItemAdded(SPItemEventProperties properties)
...{
SPListItem item = properties.ListItem;
item.BreakRoleInheritance(true);
}
}
}
以上代码的意思是:当一个item被添加后,执行取消继承权限并复制父权限。
4、为程序创建强签名。项目右键->属性->签名(tab)->为程序集签名->添加(不用写密码)。
5、重新生成。
6、将生成的dll文件拷贝到 local:/windows/assembly 下,记录拷贝后dll的属性。
7、创建cmd应用程序。代码如下:
using
System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication1
... {
class Program
...{
static void Main(string[] args)
...{
//获取网站
SPSite collection = new SPSite("http://pasen222/sites/fanta");
//获取顶级站点
SPWeb site = collection.OpenWeb();
//获取相应文档库列表
SPList list = site.Lists["我的文档"];
//强命名属性
string assemblyName = "BreakRoleInheritance, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b48f4cdc14ee320a";
//类名
string className = "BreakRoleInheritance.BreakItemRoleInheritance";
//添加Event handle
list.EventReceivers.Add(SPEventReceiverType.ItemAdded,assemblyName,className);
//卸载Event handle
//for (int i = list.EventReceivers.Count - 1; i >= 0;i-- )
//{
// list.EventReceivers[i].Delete();
//}
Console.WriteLine("the procedure has been installed! please press enter to continue!");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication1
... {
class Program
...{
static void Main(string[] args)
...{
//获取网站
SPSite collection = new SPSite("http://pasen222/sites/fanta");
//获取顶级站点
SPWeb site = collection.OpenWeb();
//获取相应文档库列表
SPList list = site.Lists["我的文档"];
//强命名属性
string assemblyName = "BreakRoleInheritance, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b48f4cdc14ee320a";
//类名
string className = "BreakRoleInheritance.BreakItemRoleInheritance";
//添加Event handle
list.EventReceivers.Add(SPEventReceiverType.ItemAdded,assemblyName,className);
//卸载Event handle
//for (int i = list.EventReceivers.Count - 1; i >= 0;i-- )
//{
// list.EventReceivers[i].Delete();
//}
Console.WriteLine("the procedure has been installed! please press enter to continue!");
Console.ReadLine();
}
}
}
8、重新生成,生成exe文件,并执行。
好了,到文档(我的文档)中看看,当创建一个文档后,是不是已经取消继承权限了呢?其实SPItemEventReceiver下面还有好多好多的方法,足够在任何条件下出发事件了。
以上就是我憋了好多天才想到的另一种抛开工作流解决问题的方法。