Ejemplo n.º 1
0
 public ApiList <SPView> List(SPList list)
 {
     using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
     {
         SP.ViewCollection viewCollection = clientContext.ToList(list.Id).Views;
         var     views       = clientContext.LoadQuery(viewCollection.Include(SPView.InstanceQuery));
         SP.List splist      = clientContext.ToList(list.Id);
         var     fieldsQuery = clientContext.LoadQuery(splist.Fields.Include(
                                                           field => field.Title,
                                                           field => field.InternalName));
         clientContext.Load(splist,
                            _list => _list.ContentTypes.Include(ct => ct.Fields.SchemaXml),
                            _list => _list.SchemaXml);
         clientContext.ExecuteQuery();
         var spviewCollection = new ApiList <SPView>();
         var columns          = fieldsQuery.ToDictionary(item => item.InternalName, item => item.Title);
         foreach (var view in views)
         {
             if (!view.Hidden)
             {
                 spviewCollection.Add(new SPView(view, columns));
             }
         }
         return(spviewCollection);
     }
 }
        public void Delete(SPList list, SPListItem item)
        {
            cacheService.RemoveByTags(new[] { GetTag(list.Id) }, CacheScope.Context | CacheScope.Process);

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.List     spList = clientContext.ToList(list.Id);
                SP.ListItem spItem = clientContext.ToList(list.Id).GetItemById(item.Id);
                spItem.DeleteObject();
                clientContext.ExecuteQuery();
            }
        }
 public bool IsFolderValid(SPList list, string folderName, string currentDir)
 {
     folderName = folderName.Trim();
     if (String.IsNullOrEmpty(folderName))
     {
         return(false);
     }
     using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
     {
         SP.List   splist       = clientContext.ToList(list.Id);
         SP.Folder parentFolder = clientContext.Web.GetFolderByServerRelativeUrl(currentDir);
         clientContext.Load(parentFolder);
         var subfolders = clientContext.LoadQuery(parentFolder.Folders.Where(folder => folder.Name == folderName));
         try
         {
             clientContext.ExecuteQuery();
         }
         catch (Exception ex)
         {
             EventLogs.Warn(String.Format("An exception of type {0} occurred while loading subfolders for a directory '{1}'. The exception message is: {2}", ex.GetType().Name, currentDir, ex.Message), "SharePointClient", 778, CSContext.Current.SettingsID);
             return(false);
         }
         // subfolders.Count()>0 means, that the folder already exists
         return(!subfolders.Any());
     }
 }
        public SP.Folder NewFolder(SPList list, string folderName, string currentDir)
        {
            if (!IsFolderValid(list, folderName, currentDir))
            {
                return(null);
            }

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.List   splist       = clientContext.ToList(list.Id);
                SP.Folder parentFolder = clientContext.Web.GetFolderByServerRelativeUrl(currentDir);
                clientContext.Load(parentFolder);

                // add a new folder
                SP.Folder newFolder = parentFolder.Folders.Add(folderName);
                parentFolder.Update();
                clientContext.Load(newFolder);
                try
                {
                    clientContext.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    EventLogs.Warn(String.Format("An exception of type {0} occurred while creating a new folder with a name '{1}' for a directory '{2}'. The exception message is: {3}", ex.GetType().Name, folderName, currentDir, ex.Message), "SharePointClient", 778, CSContext.Current.SettingsID);
                    return(null);
                }

                cacheService.RemoveByTags(new[] { GetTag(list.Id) }, CacheScope.Context | CacheScope.Process);
                return(newFolder);
            }
        }
Ejemplo n.º 5
0
 public SPDocumentCheckOutInfo GetFileInfo(SPList list, SPListItem listItem)
 {
     using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
     {
         var spList = clientContext.ToList(list.Id);
         var spfile = spList.GetItemById(listItem.Id).File;
         clientContext.Load(spList);
         clientContext.Load(spfile);
         clientContext.Load(spfile, f => f.CheckedOutByUser);
         clientContext.ExecuteQuery();
         User user = null;
         if (!spfile.CheckedOutByUser.ServerObjectIsNull.GetValueOrDefault(true))
         {
             user = spfile.CheckedOutByUser;
             clientContext.Load(user);
             clientContext.ExecuteQuery();
         }
         return(new SPDocumentCheckOutInfo
         {
             IsCheckedOut = !(spfile.CheckedOutByUser.ServerObjectIsNull ?? true),
             CheckedOutByUser = user,
             EnableVersioning = spList.EnableVersioning,
             EnableMinorVersions = spList.EnableMinorVersions,
             MajorVersion = spfile.MajorVersion,
             MinorVersion = spfile.MinorVersion
         });
     }
 }
        public SP.FileCollection Attachments(SPListItem listItem, SPList list, object value)
        {
            if (value == null || !(bool)value)
            {
                return(null);
            }

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.Web web = clientContext.Web;
                clientContext.Load(web);
                SP.List splist = clientContext.ToList(list.Id);
                clientContext.Load(splist);
                SP.ListItem splistItem = splist.GetItemById(listItem.Id);
                clientContext.Load(splistItem);
                clientContext.ExecuteQuery();

                SP.Folder listFolder = splistItem.ParentList.RootFolder;
                clientContext.Load(listFolder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                SP.Folder attachmentsFolder = web.GetFolderByServerRelativeUrl(listFolder.ServerRelativeUrl + "/attachments/" + splistItem.Id.ToString());
                clientContext.Load(attachmentsFolder);
                var attachments = attachmentsFolder.Files;
                clientContext.Load(attachments);
                clientContext.ExecuteQuery();

                return(attachments);
            }
        }
Ejemplo n.º 7
0
        public SPView Get(
            [Documentation(Name = "List", Type = typeof(SPList)),
             Documentation(Name = "ById", Type = typeof(string)),
             Documentation(Name = "ByTitle", Type = typeof(string))]
            IDictionary options)
        {
            SPList list = null;

            if (options != null && options["List"] != null)
            {
                list = (SPList)options["List"];
            }
            else
            {
                return(null);
            }

            var byId    = (options["ById"] != null) ? options["ById"].ToString() : string.Empty;
            var byTitle = (options["ByTitle"] != null) ? options["ByTitle"].ToString() : string.Empty;

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.List splist      = clientContext.ToList(list.Id);
                var     fieldsQuery = clientContext.LoadQuery(splist.Fields.Include(
                                                                  field => field.Title,
                                                                  field => field.InternalName));

                SP.View spview = null;

                if (!string.IsNullOrEmpty(byId))
                {
                    string viewId = options["ById"].ToString();
                    spview = splist.GetView(new Guid(viewId));
                    clientContext.Load(spview);
                    clientContext.Load(spview, SPView.InstanceQuery);
                    clientContext.ExecuteQuery();
                }

                if (spview == null && !string.IsNullOrEmpty(byTitle))
                {
                    string viewTitle = options["ByTitle"].ToString();
                    var    viewQuery = clientContext.LoadQuery(splist.Views
                                                               .Where(view => view.Title == viewTitle)
                                                               .IncludeWithDefaultProperties(SPView.InstanceQuery));
                    clientContext.ExecuteQuery();
                    spview = viewQuery.FirstOrDefault();
                }

                if (spview != null)
                {
                    return(new SPView(spview, fieldsQuery.ToDictionary(item => item.InternalName, item => item.Title)));
                }
            }

            return(null);
        }
 public SPListItem Update(SPList list, SPListItem item)
 {
     using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
     {
         SP.List     splist   = clientContext.ToList(list.Id);
         SP.ListItem listItem = clientContext.ToList(list.Id).GetItemById(item.Id);
         clientContext.Load(listItem);
         foreach (var field in item.Fields)
         {
             listItem[field.Key] = field.Value;
         }
         clientContext.ValidateOnClient = true;
         listItem.Update();
         clientContext.Load(listItem);
         var fieldsQuery = clientContext.LoadQuery(splist.Fields.Where(field => !field.Hidden && field.Group != "_Hidden"));
         clientContext.ExecuteQuery();
         cacheService.RemoveByTags(new[] { GetTag(list.Id) }, CacheScope.Context | CacheScope.Process);
         return(new SPListItem(listItem, fieldsQuery.ToList()));
     }
 }
Ejemplo n.º 9
0
        public void UndoCheckOut(SPList list, SPListItem listItem)
        {
            RemoveFileCache(list, listItem);

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                var spfile = clientContext.ToList(list.Id).GetItemById(listItem.Id).File;
                spfile.UndoCheckOut();
                clientContext.ExecuteQuery();
            }
        }
Ejemplo n.º 10
0
        public void CheckInFile(SPList list, SPListItem listItem)
        {
            RemoveFileCache(list, listItem);

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                var spfile = clientContext.ToList(list.Id).GetItemById(listItem.Id).File;
                spfile.CheckIn(String.Empty, CheckinType.MajorCheckIn);
                clientContext.ExecuteQuery();
            }
        }
        public SPListItem Create(SPList list)
        {
            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                var splist     = clientContext.ToList(list.Id);
                var splistItem = splist.AddItem(null);
                splistItem.Update();
                clientContext.Load(splistItem, SPItemService.InstanceQuery);
                var fieldsQuery = clientContext.LoadQuery(splist.Fields.Where(field => !field.Hidden && field.Group != "_Hidden"));
                clientContext.ExecuteQuery();

                cacheService.RemoveByTags(new[] { GetTag(list.Id) }, CacheScope.Context | CacheScope.Process);
                return(new SPListItem(splistItem, fieldsQuery.ToList()));
            }
        }
Ejemplo n.º 12
0
        public void CheckInFile(SPList list, SPListItem listItem, string checkinType, bool keepCOut, string comment)
        {
            RemoveFileCache(list, listItem);

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                var spfile = clientContext.ToList(list.Id).GetItemById(listItem.Id).File;
                var type   = (CheckinType)Enum.Parse(typeof(CheckinType), checkinType);
                spfile.CheckIn(comment, type);
                if (keepCOut)
                {
                    spfile.CheckOut();
                }
                clientContext.ExecuteQuery();
            }
        }
        public SPListItem Get(SPList list, string listItemId,
                              [Documentation(Name = "ViewFields", Type = typeof(List <string>))]
                              IDictionary options)
        {
            int id;

            if (!int.TryParse(listItemId, out id))
            {
                return(null);
            }

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                var splist = clientContext.ToList(list.Id);
                IEnumerable <ListItem> itemQuery;
                if (options != null && options["ViewFields"] != null)
                {
                    var viewFields = (List <string>)options["ViewFields"];
                    itemQuery = clientContext.LoadQuery(splist.GetItems(CamlQuery.CreateAllItemsQuery())
                                                        .Where(item => item.Id == id)
                                                        .Include(CreateListItemLoadExpressions(viewFields)));
                }
                else
                {
                    itemQuery = clientContext.LoadQuery(splist.GetItems(CamlQuery.CreateAllItemsQuery())
                                                        .Where(item => item.Id == id)
                                                        .IncludeWithDefaultProperties(SPItemService.InstanceQuery));
                }
                var fieldsQuery = clientContext.LoadQuery(splist.Fields.Where(field => !field.Hidden && field.Group != "_Hidden"));
                clientContext.ExecuteQuery();
                var listItem = itemQuery.FirstOrDefault();
                if (listItem != null)
                {
                    return(new SPListItem(listItem, fieldsQuery.ToList()));
                }
                return(null);
            }
        }
Ejemplo n.º 14
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);
            }
        }
        public ApiList <SPListItem> List(SPList list,
                                         [Documentation(Name = "ViewFields", Type = typeof(List <string>)),
                                          Documentation(Name = "ViewQuery", Type = typeof(string))]
                                         IDictionary options)
        {
            if (list == null)
            {
                return(new ApiList <SPListItem>());
            }

            var cacheId   = string.Concat(SharePointListItemCacheId, string.Format("{0}_{1}", list.SPWebUrl, list.Id));
            var cacheList = (ApiList <SPListItem>)cacheService.Get(cacheId, CacheScope.Context | CacheScope.Process);

            if (cacheList == null)
            {
                using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
                {
                    var sharepointList = clientContext.ToList(list.Id);
                    ListItemCollection listItemCollection;
                    if (options != null && options["ViewQuery"] != null)
                    {
                        var camlQuery = new CamlQuery
                        {
                            ViewXml = String.Format("<View><Query>{0}</Query></View>", options["ViewQuery"])
                        };
                        listItemCollection = sharepointList.GetItems(camlQuery);
                    }
                    else
                    {
                        listItemCollection = sharepointList.GetItems(CamlQuery.CreateAllItemsQuery());
                    }
                    IEnumerable <ListItem> items;
                    if (options != null && options["ViewFields"] != null)
                    {
                        var viewFields = (List <string>)options["ViewFields"];
                        items = clientContext.LoadQuery(listItemCollection.Include(CreateListItemLoadExpressions(viewFields)));
                    }
                    else
                    {
                        items = clientContext.LoadQuery(listItemCollection.Include(SPItemService.InstanceQuery));
                    }
                    var userItemCollection = clientContext.Web.SiteUserInfoList.GetItems(CamlQuery.CreateAllItemsQuery());
                    IEnumerable <SP.ListItem> userItems = clientContext.LoadQuery(
                        userItemCollection.Include(new Expression <Func <ListItem, object> >[] { item => item.Id, item => item["Picture"] }));

                    var fieldsQuery = clientContext.LoadQuery(sharepointList.Fields.Where(field => !field.Hidden && field.Group != "_Hidden"));
                    clientContext.ExecuteQuery();
                    var apiList = new ApiList <SPListItem>();
                    var fields  = fieldsQuery.ToList();

                    foreach (var item in items)
                    {
                        apiList.Add(new SPListItem(item, fields));
                    }

                    cacheService.Put(cacheId, apiList, CacheScope.Context | CacheScope.Process, new[] { GetTag(list.Id) }, CacheTimeOut);
                    return(apiList);
                }
            }

            return(cacheList);
        }