码迷,mamicode.com
首页 > 其他好文 > 详细

sharepoint给文档授权

时间:2014-08-15 12:11:08      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:os   io   strong   文件   for   ar   cti   log   

 //在列表根目录下创建文件夹

        public static string CreatFolderToSPDocLib(string strFolderName, string strDocLibName)

        {

            string FolderPath = string.Empty;

 

            try

            {

                using (SPSite site = new SPSite(SiteUrl))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        web.AllowUnsafeUpdates = true;

                        SPListCollection lists = web.GetListsOfType(SPBaseType.DocumentLibrary);

                        lists.IncludeRootFolder = true;

                        SPList list = lists[strDocLibName];

                        list.EnableFolderCreation = true;

                        SPListItem item = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);

                        item.Update();

                        list.Update();

                        FolderPath = item["FileRef"].ToString();

                        web.AllowUnsafeUpdates = false;

                    }

                }

            }

            catch

            {

            }

            return FolderPath;

        }

 

  //上传文件到文件夹,并授权给相关用户

        public static bool UpLoadFileToFolder(byte[] FileStream, string FileName, string FolderPath, string allLoginName)

        {

            try

            {

                using (SPSite site = new SPSite(SiteUrl))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        web.AllowUnsafeUpdates = true;

                        SPFolder folder = web.GetFolder(FolderPath);

 

                        SPListItem listItem = folder.Files.Add(FileName, FileStream).Item;

 

                        //断开原来列表项所继承的权限,使其可以设置独立权限

                        listItem.BreakRoleInheritance(true);

                        //将原来所继承的权限通通移除

                        foreach (SPRoleAssignment roleAssignment in listItem.RoleAssignments)

                        {

                            roleAssignment.RoleDefinitionBindings.RemoveAll();

                            roleAssignment.Update();

                            listItem.Update();

                        }

                        //获取将要设置权限的用户

                        SPUser myUser = web.EnsureUser(allLoginName);

                        //定义权限分配

                        SPRoleAssignment myRoleAssignment = new SPRoleAssignment(myUser.LoginName, myUser.Email, myUser.Name, myUser.Notes);

                        //绑定设置的权限

                        myRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Reader));

                        //把这个权限加到我们的列表中

                        listItem.RoleAssignments.Add(myRoleAssignment);

                        listItem.Update();

 

                        web.AllowUnsafeUpdates = false;

                        return true;

                    }

                }

            }

            catch

            {

                return false;

            }

        }

 

        //通过ID获取列表项

        public static string GetRoleAssignmentsOfSPListItem(string ListName, int ItemID)

        {

            string reValue = string.Empty;

            try

            {

                using (SPSite site = new SPSite(SiteUrl))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        web.AllowUnsafeUpdates = true;

                        SPList list = web.Lists[ListName];

                        SPListItem item = list.Items.GetItemById(ItemID);

                        SPRoleAssignmentCollection Rolecoll = item.RoleAssignments;

                        foreach (SPRoleAssignment role in Rolecoll)

                        {

                            for (int i = 0; i < role.RoleDefinitionBindings.Count; i++)

                            {

                                reValue += (role.Member.LoginName + ":" + role.RoleDefinitionBindings[i].Name + ":" + role.RoleDefinitionBindings[i].BasePermissions.ToString());

                            }

                        }

 

                        web.AllowUnsafeUpdates = false;

                    }

                }

            }

            catch

            {

            }

            return reValue;

        }

 

sharepoint给文档授权,布布扣,bubuko.com

sharepoint给文档授权

标签:os   io   strong   文件   for   ar   cti   log   

原文地址:http://www.cnblogs.com/ruiling/p/3914159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!