osh*_*nen 13 .net c# sharepoint sharepoint-2010 csom
文件夹工作:
我现在知道如何设置库中文件夹的权限:
public void ChangeItemPermissions()
{
_SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://sharepoint.oshirowanen.com/sites/oshirodev/");
_ClientContext.Credentials = new NetworkCredential("user", "pass", "oshirowanen.com");
_SharePoint.Principal user = _ClientContext.Web.EnsureUser(@"oshirowanen\tom");
var _List = _ClientContext.Web.Lists.GetByTitle("Library1");
var _Item = _List.LoadItemByUrl("/sites/oshirodev/Library1/Folder1");
var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);
var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
_Item.BreakRoleInheritance(false,true);
_Item.RoleAssignments.Add(user, roleBindings);
_ClientContext.ExecuteQuery();
}
Run Code Online (Sandbox Code Playgroud)
文件尝试:
我已经尝试将文件名添加到此行:
var _Item = _List.LoadItemByUrl("/sites/oshirodev/Library1/Folder1/File1.docx");
注意(/File1.docx)添加到上面一行的末尾.
收到错误:
但这只是一个错误:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=ItemPermissions
StackTrace:
at ItemPermissions.Form1.ChangeItemPermissions() in c:\Users\Oshirowanen\Documents\Visual Studio 2013\Projects\ItemPermissions\ItemPermissions\Form1.cs:line 46
at ItemPermissions.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\Oshirowanen\Documents\Visual Studio 2013\Projects\ItemPermissions\ItemPermissions\Form1.cs:line 345
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ItemPermissions.Program.Main() in c:\Users\Oshirowanen\Documents\Visual Studio 2013\Projects\ItemPermissions\ItemPermissions\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Run Code Online (Sandbox Code Playgroud)
相关信息:
这是一个在本地计算机上运行的WinForm应用程序,使用.NET 4.0使用C#创建.SharePoint版本是2010年.
问题:
如何设置特定文件的权限?我已经知道如何为特定文件夹设置权限.
小智 2
我认为andresm53您使用他提到的扩展方法是正确的。
要使此方法适用于您需要按(文件名)查询的文件FileLeafRef,并确保添加FileLeafRef到 query ViewFields。
/编辑
还有一件非常重要的事情,将文件查询限制到确切的文件夹:
query.FolderServerRelativeUrl = System.IO.Path.GetDirectoryName(url).Replace("\\", "/");
Run Code Online (Sandbox Code Playgroud)
工作代码如下:
void Main()
{
ClientContext context = new ClientContext("https://sharepoint.oshirowanen.com/sites/oshirodev/");
context.Credentials = new NetworkCredential("user", "pass", "oshirowanen.com");
Principal user = context.Web.EnsureUser(@"oshirowanen\tom");
ChangeItemPermissions(context, "/sites/oshirodev/Library1/Folder1/File1.docx", "Library1", user, RoleType.Reader);
}
// Define other methods and classes here
public static ListItem LoadItemByUrl(List list, string url)
{
var context = list.Context;
string ext = System.IO.Path.GetExtension(url);
var query = new CamlQuery();
if (string.IsNullOrEmpty(ext))
query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='FileRef'/><Value> Type='Url'>{0}</Value></Eq></Where></Query></View>", url);
else
{
query.ViewXml = String.Format("<View><ViewFields><FieldRef Name=\"FileLeafRef\" /></ViewFields><Query><Where><Eq><FieldRef Name=\"FileLeafRef\" /><Value Type=\"Text\">{0}</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>", System.IO.Path.GetFileName(url));
query.FolderServerRelativeUrl = System.IO.Path.GetDirectoryName(url).Replace("\\", "/");
}
var items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
return items.Count > 0 ? items[0] : null;
}
public void ChangeItemPermissions(ClientContext context, string url, string listName, Principal user, RoleType type)
{
var list = context.Web.Lists.GetByTitle(listName);
var item = LoadItemByUrl(list, url);
var roleDefinition = context.Site.RootWeb.RoleDefinitions.GetByType(type);
var roleBindings = new RoleDefinitionBindingCollection(context) { roleDefinition };
item.BreakRoleInheritance(false, true);
item.RoleAssignments.Add(user, roleBindings);
context.ExecuteQuery();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
909 次 |
| 最近记录: |