Ejemplo n.º 1
0
        public SPPermissions Get(int userOrGroupId, PermissionsGetQuery options)
        {
            var spwebUrl = EnsureUrl(options.Url, options.ListId);

            try
            {
                using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                {
                    List splist = clientContext.Web.Lists.GetById(options.ListId);
                    var  splistItemCollection = splist.GetItems(options.Id.HasValue ?
                                                                CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) :
                                                                CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));

                    var listItemRoleAssignments = clientContext.LoadQuery(
                        splistItemCollection.Select(item => item.RoleAssignments.GetByPrincipalId(userOrGroupId)).Include(
                            roleAssignment => roleAssignment.Member,
                            roleAssignment => roleAssignment.RoleDefinitionBindings.Include(
                                roleDef => roleDef.Id,
                                roleDef => roleDef.Name,
                                roleDef => roleDef.Description)));

                    clientContext.ExecuteQuery();

                    return(listItemRoleAssignments.First().ToPermission());
                }
            }
            catch (Exception ex)
            {
                string itemId  = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString();
                string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.Get() method for a User or Group with Id: {1} ListId: {2} ItemId: {3}. The exception message is: {4}", ex.GetType(), userOrGroupId, options.ListId, itemId, ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }
        }
Ejemplo n.º 2
0
        public void ResetInheritance(PermissionsGetQuery options)
        {
            var spwebUrl = EnsureUrl(options.Url, options.ListId);

            try
            {
                using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                {
                    List splist = clientContext.Web.Lists.GetById(options.ListId);
                    var  splistItemCollection = splist.GetItems(options.Id.HasValue ?
                                                                CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) :
                                                                CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));

                    var lazyListItems = clientContext.LoadQuery(splistItemCollection.Include(item => item.HasUniqueRoleAssignments, item => item.Id));
                    clientContext.ExecuteQuery();

                    var splistItem = lazyListItems.First();
                    splistItem.ResetRoleInheritance();

                    clientContext.ExecuteQuery();
                }
            }
            catch (Exception ex)
            {
                string itemId  = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString();
                string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.ResetInheritance() method for ListId: {1} ItemId: {2}. The exception message is: {3}", ex.GetType(), options.ListId, itemId, ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }
        }
        public void Remove(string url, Guid listId, AttachmentsRemoveQuery options)
        {
            using (var clientContext = new SPContext(url, credentials.Get(url)))
            {
                ListItemCollection listItems = null;
                if (!options.Id.HasValue)
                {
                    listItems = clientContext.Web.Lists.GetById(listId).GetItems(CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));
                    clientContext.Load(listItems, _ => _.Include(item => item.Id));
                }

                var listRootFolder = clientContext.Web.Lists.GetById(listId).RootFolder;
                clientContext.Load(listRootFolder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                int listItemId = options.Id.HasValue ? options.Id.Value : listItems.First().Id;

                var attachmentsFolder = clientContext.Web.GetFolderByServerRelativeUrl(listRootFolder.ServerRelativeUrl + "/Attachments/" + listItemId);
                clientContext.Load(attachmentsFolder.Files);
                clientContext.ExecuteQuery();

                foreach (var file in attachmentsFolder.Files.ToList())
                {
                    if (options.FileNames.Contains(file.Name))
                    {
                        file.DeleteObject();
                    }
                }
                attachmentsFolder.Update();
                clientContext.ExecuteQuery();
            }
        }
        public void Add(string url, Guid listId, AttachmentsAddQuery options)
        {
            using (var clientContext = new SPContext(url, credentials.Get(url)))
            {
                ListItemCollection listItems = null;
                if (!options.Id.HasValue)
                {
                    listItems = clientContext.Web.Lists.GetById(listId).GetItems(CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));
                    clientContext.Load(listItems, _ => _.Include(item => item.Id));
                }

                var listRootFolder = clientContext.Web.Lists.GetById(listId).RootFolder;
                clientContext.Load(listRootFolder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                int listItemId = options.Id.HasValue ? options.Id.Value : listItems.First().Id;

                Microsoft.SharePoint.Client.Folder attachmentsFolder;
                try
                {
                    attachmentsFolder = clientContext.Web.GetFolderByServerRelativeUrl(listRootFolder.ServerRelativeUrl + "/Attachments/" + listItemId.ToString(CultureInfo.InvariantCulture).ToLowerInvariant());
                    clientContext.Load(attachmentsFolder);
                    clientContext.ExecuteQuery();
                }
                catch
                {
                    attachmentsFolder = null;
                }

                // add
                var useService = (attachmentsFolder == null);
                if (useService)
                {
                    //There is no way to create attachments folder using client object model.
                    using (var service = new ListService(url, credentials.Get(url)))
                    {
                        service.AddAttachment(listId.ToString(), listItemId.ToString(CultureInfo.InvariantCulture), options.FileName, options.FileData);
                    }
                }
                else
                {
                    var fileUrl = string.Format("{0}/{1}", listRootFolder.ServerRelativeUrl + "/Attachments/" + listItemId.ToString(CultureInfo.InvariantCulture).ToLowerInvariant(), options.FileName);
                    if (options.FileData != null)
                    {
                        using (var stream = new MemoryStream(options.FileData))
                        {
                            Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, stream, true);
                        }
                        clientContext.ExecuteQuery();
                    }
                }
            }
        }
        public List <SPAttachment> List(string url, Guid listId, AttachmentsGetQuery options)
        {
            using (var clientContext = new SPContext(url, credentials.Get(url)))
            {
                var site = clientContext.Site;
                clientContext.Load(site, s => s.Url);

                ListItemCollection listItems = null;
                if (!options.Id.HasValue)
                {
                    listItems = clientContext.Web.Lists.GetById(listId).GetItems(CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));
                    clientContext.Load(listItems, _ => _.Include(item => item.Id));
                }

                var listRootFolder = clientContext.Web.Lists.GetById(listId).RootFolder;
                clientContext.Load(listRootFolder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                try
                {
                    int listItemId = options.Id.HasValue ? options.Id.Value : listItems.First().Id;

                    var attachmentsFolder = clientContext.Web.GetFolderByServerRelativeUrl(listRootFolder.ServerRelativeUrl + "/Attachments/" + listItemId.ToString(CultureInfo.InvariantCulture).ToLowerInvariant());
                    clientContext.Load(attachmentsFolder);
                    clientContext.Load(attachmentsFolder.Files,
                                       files => files.Include(
                                           f => f.ServerRelativeUrl,
                                           f => f.Name,
                                           f => f.Title,
                                           f => f.TimeCreated,
                                           f => f.Author,
                                           f => f.TimeLastModified,
                                           f => f.ModifiedBy));
                    clientContext.ExecuteQuery();
                    return(attachmentsFolder.Files.ToList().Select(attachment => new SPAttachment(attachment.Name, new Uri(site.Url + attachment.ServerRelativeUrl))
                    {
                        Created = attachment.TimeCreated,
                        Modified = attachment.TimeLastModified,
                        CreatedBy = new SPUserPrincipal(attachment.Author),
                        ModifiedBy = new SPUserPrincipal(attachment.ModifiedBy)
                    }).ToList());
                }
                catch
                {
                    return(new List <SPAttachment>());
                }
            }
        }
Ejemplo n.º 6
0
 private static SP.CamlQuery GetFolderByServerRelativeUrl(string serverRelativeUrl)
 {
     return(CAMLQueryBuilder.GetQuery(1, new[] { "Id", "UniqueId", "FileRef", "Modified" }, string.Format(@"
         <Where>
             <And>
                 <Eq>
                     <FieldRef Name='ContentType'/>
                     <Value Type='Text'>Folder</Value>
                 </Eq>
                 <Eq>
                     <FieldRef Name='FileRef'/>
                     <Value Type='Text'>{0}</Value>
                 </Eq>
             </And>
         </Where>", serverRelativeUrl), queryAllFoldersAndSubFolders: true));
 }
Ejemplo n.º 7
0
        public PagedList <SPPermissions> List(PermissionsListQuery options)
        {
            var spwebUrl = EnsureUrl(options.Url, options.ListId);

            try
            {
                using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                {
                    List splist = clientContext.Web.Lists.GetById(options.ListId);
                    var  splistItemCollection = splist.GetItems(options.Id.HasValue ?
                                                                CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) :
                                                                CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));

                    var lazyListItems = clientContext.LoadQuery(splistItemCollection.Include(item => item.HasUniqueRoleAssignments, item => item.Id));
                    clientContext.ExecuteQuery();

                    var splistItem = lazyListItems.First();
                    IEnumerable <RoleAssignment> lazyRoleAssignmentList = RoleAssignmentsLoadQuery(splistItem, clientContext);
                    clientContext.ExecuteQuery();

                    var roleAssignmentList = lazyRoleAssignmentList.ToList();
                    return(new PagedList <SPPermissions>(roleAssignmentList.Skip(options.PageSize * options.PageIndex).Take(options.PageSize).Select(ra => ra.ToPermission()))
                    {
                        PageSize = options.PageSize,
                        PageIndex = options.PageIndex,
                        TotalCount = roleAssignmentList.Count
                    });
                }
            }
            catch (Exception ex)
            {
                string listItemId = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString();
                string message    = string.Format("An exception of type {0} occurred in the SPPermissionsService.List() method for ListId: {1} ItemId: {2}. The exception message is: {3}", ex.GetType(), options.ListId, listItemId, ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }
        }
Ejemplo n.º 8
0
        public void Remove(int[] userOrGroupIds, PermissionsGetQuery options)
        {
            if (userOrGroupIds != null && userOrGroupIds.Length > 0)
            {
                var spwebUrl = EnsureUrl(options.Url, options.ListId);

                try
                {
                    using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                    {
                        List splist = clientContext.Web.Lists.GetById(options.ListId);
                        var  splistItemCollection = splist.GetItems(options.Id.HasValue ?
                                                                    CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) :
                                                                    CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));

                        var lazyListItems = clientContext.LoadQuery(splistItemCollection.Include(item => item.HasUniqueRoleAssignments, item => item.Id));
                        clientContext.ExecuteQuery();

                        var splistItem = lazyListItems.First();
                        foreach (int userOrGroupId in userOrGroupIds)
                        {
                            splistItem.RoleAssignments.GetByPrincipalId(userOrGroupId).DeleteObject();
                        }
                        clientContext.ExecuteQuery();
                    }
                }
                catch (Exception ex)
                {
                    string listItemId           = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString();
                    string userOrGroupIdsString = string.Join(", ", userOrGroupIds);
                    string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.Remove() method for Users or Groups with Ids: {1} ListId: {2} ItemId: {3}. The exception message is: {4}", ex.GetType(), userOrGroupIdsString, options.ListId, listItemId, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    throw new SPInternalException(message, ex);
                }
            }
        }
Ejemplo n.º 9
0
        public void Update(PermissionsUpdateQuery options)
        {
            var spwebUrl = EnsureUrl(options.Url, options.ListId);

            try
            {
                using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                {
                    List splist = clientContext.ToList(options.ListId);
                    var  splistItemCollection = splist.GetItems(options.Id.HasValue ?
                                                                CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) :
                                                                CAMLQueryBuilder.GetItem(options.ContentId, new string[] { }));

                    var lazyListItems = clientContext.LoadQuery(splistItemCollection.Include(item => item.HasUniqueRoleAssignments, item => item.Id));
                    clientContext.ExecuteQuery();

                    var splistItem = lazyListItems.First();
                    splistItem.BreakRoleInheritance(options.CopyRoleAssignments, options.ClearSubscopes);

                    if (options.Levels != null)
                    {
                        var rdList = new RoleDefinitionBindingCollection(clientContext);
                        foreach (var permissionLevelId in options.Levels)
                        {
                            rdList.Add(clientContext.Web.RoleDefinitions.GetById(permissionLevelId));
                        }

                        if (options.GroupIds != null)
                        {
                            foreach (var groupId in options.GroupIds)
                            {
                                var group = clientContext.Web.SiteGroups.GetById(groupId);
                                if (options.Overwrite)
                                {
                                    splistItem.RoleAssignments.GetByPrincipal(group).DeleteObject();
                                }
                                splistItem.RoleAssignments.Add(group, rdList);
                            }
                        }

                        if (options.LoginNames != null)
                        {
                            foreach (var loginName in options.LoginNames)
                            {
                                var user = clientContext.Web.EnsureUser(loginName);
                                if (options.Overwrite)
                                {
                                    splistItem.RoleAssignments.GetByPrincipal(user).DeleteObject();
                                }
                                splistItem.RoleAssignments.Add(user, rdList);
                            }
                        }
                    }

                    clientContext.ExecuteQuery();
                }
            }
            catch (Exception ex)
            {
                string listItemId = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString();
                string message    = string.Format("An exception of type {0} occurred in the SPPermissionsService.Update() method for ListId: {1} ItemId: {2}. The exception message is: {3}", ex.GetType(), options.ListId, listItemId, ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }
        }