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); } }
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 SP.Folder NewFolder(string url, Guid listId, string folderName, string currentDir) { if (!IsFolderValid(url, listId, folderName, currentDir)) { return(null); } using (var clientContext = new SPContext(url, credentials.Get(url))) { 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) { SPLog.FileNotFound(ex, "Error occurred while creating a new folder with a name '{0}' for a directory '{1}'.", folderName, currentDir); return(null); } CacheService.RemoveByTags(new[] { Documents.Tag(listId), Folders.Tag(listId) }, CacheScope.All); return(newFolder); } }
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 IRestResponse Get(SPViewItemRequest request) { var response = new DefaultRestResponse { Name = "View" }; var errors = new List <string>(); ValidateUrl(request.Url, errors); var listId = ValidateListId(request.ListId, errors); var viewId = ValidateViewId(request.ViewId, errors); if (errors.Any()) { response.Errors = errors.ToArray(); return(response); } response.Data = cacheService.Get(CacheKey(listId, viewId, request.Url), CacheScope.Context | CacheScope.Process); if (response.Data == null) { try { using (var clientContext = new SPContext(request.Url, IntegrationManagerPlugin.CurrentAuth(request.Url), runAsServiceAccount: true)) { var site = clientContext.Site; clientContext.Load(site, s => s.Id); var web = clientContext.Web; clientContext.Load(web, w => w.Id); var list = clientContext.Web.Lists.GetById(listId); var view = list.GetView(viewId); clientContext.Load(view, RestSPView.InstanceQuery); clientContext.ExecuteQuery(); response.Data = new SPViewItemData { Item = new RestSPView(view), SiteId = site.Id, WebId = web.Id }; } cacheService.Put(CacheKey(listId, viewId, request.Url), response.Data, CacheScope.Context | CacheScope.Process, new string[] { }, CacheTimeOut); } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the RESTApi.Views.Get() method for ListId: {1} ViewId: {2} SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), listId, viewId, request.Url, ex.Message); SPLog.RoleOperationUnavailable(ex, message); response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) }; } } return(response); }
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 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 Folder GetParent(string url, Guid libraryId, string folderPath) { if (string.IsNullOrEmpty(folderPath) || string.IsNullOrEmpty(folderPath.Trim('/'))) { return(null); } try { using (var clientContext = new SPContext(url, credentials.Get(url))) { SP.List splist = clientContext.Web.Lists.GetById(libraryId); SP.Folder rootFolder = splist.RootFolder; clientContext.Load(rootFolder, f => f.ServerRelativeUrl); clientContext.ExecuteQuery(); if (rootFolder.ServerRelativeUrl.Trim('/').Equals(folderPath.Trim('/'), StringComparison.InvariantCultureIgnoreCase)) { // This is a root folder, so it has no parents return(null); } var spfolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); clientContext.Load(spfolder, f => f.ServerRelativeUrl); clientContext.Load(spfolder.ParentFolder, p => p.Name, p => p.ServerRelativeUrl, p => p.ItemCount); clientContext.ExecuteQuery(); bool pathIsNotEmpty = spfolder != null && !String.IsNullOrEmpty(spfolder.ServerRelativeUrl.Trim('/')); bool isARootFolder = spfolder == null || rootFolder.ServerRelativeUrl.Trim('/').Equals(spfolder.ServerRelativeUrl.Trim('/')); bool hasParentFolder = pathIsNotEmpty && !isARootFolder; if (!hasParentFolder) { return(null); } if (spfolder.ParentFolder != null) { return(new Folder(spfolder.ParentFolder.Name, spfolder.ParentFolder.ServerRelativeUrl, spfolder.ParentFolder.ItemCount, libraryId)); } return(null); } } catch (Exception ex) { throw new SPInternalException(ex.Message, ex); } }
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>()); } } }
public bool IsSite() { try { using (var clientContext = new SPContext(siteUrl, auth, runAsServiceAccount: true)) { var web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); } } catch (ClientRequestException) { // site does not existed return(false); } catch (WebException) { // credentials are invalid return(false); } catch (Exception) { // unhandled exception return(false); } return(true); }
public List <string> LoadSubSiteUrls() { var webCollection = new List <string>(); using (var clientContext = new SPContext(siteUrl, auth, runAsServiceAccount: true)) { var webs = clientContext.Web.Webs; clientContext.Load(webs); try { clientContext.ExecuteQuery(); } catch (Exception ex) { SPLog.UnKnownError(ex, "An exception in the process of SiteCollection loading of type {0} has been occurred. The exception message is: {1}", ex.GetType().Name, ex.Message); return(webCollection); } var baseUrl = clientContext.Url.Trim('/'); foreach (var web in webs) { webCollection.Add(MergeUrl(baseUrl, web.ServerRelativeUrl)); } } return(webCollection); }
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 bool IsFolderValid(string url, Guid listId, string folderName, string currentDir) { folderName = folderName.Trim(); if (String.IsNullOrEmpty(folderName)) { return(false); } using (var clientContext = new SPContext(url, credentials.Get(url))) { 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) { SPLog.FileNotFound(ex, "Coult not load subfolders for a directory '{0}'", currentDir); return(false); } return(subfolders.Any()); } }
private OAuthData GetUserData() { //We now have the credentials, so we can start making API calls using (var context = new SPContext(Office365Url, new Components.AuthenticationUtil.Methods.OAuth())) { var userDetails = context.Web.CurrentUser; context.Load(userDetails, usr => usr.Email, usr => usr.Id, usr => usr.LoginName, usr => usr.Title, usr => usr.UserId); context.ExecuteQuery(); var uid = userDetails.Id.ToString(CultureInfo.InvariantCulture); var data = new OAuthData { ClientId = uid, ClientType = ClientType, Email = userDetails.Email, UserName = SanitizeUserName(userDetails.LoginName), AvatarUrl = string.Empty, CommonName = userDetails.Title }; return(data); } }
public void Restore(string url, Guid libraryId, int itemId, string version) { try { var auth = credentials.Get(url); using (var clientContext = new SPContext(url, auth)) { var spfile = clientContext.Web.Lists.GetById(libraryId).GetItemById(itemId).File; clientContext.Load(spfile, f => f.ServerRelativeUrl); clientContext.ExecuteQuery(); var fileName = spfile.ServerRelativeUrl; // There is no way to restore file using Client Object Model using (var service = new VersionService(url, auth)) { service.RestoreVersion(fileName, version); } } } catch (Exception ex) { var message = string.Format("An exception of type {0} occurred in the InternalApi.SPFileService.Restore() method for URL: {1}, LibraryId: {2}, ItemId: {3}, Version: {4}. The exception message is: {5}", ex.GetType(), url, libraryId, itemId, version, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message); } }
public bool CanEdit(Guid listId) { var listBase = listDataService.Get(listId); if (listBase == null) { return(false); } Validate(listBase); try { bool?canEdit; using (var spcontext = new SPContext(listBase.SPWebUrl, credentials.Get(listBase.SPWebUrl))) { var splist = spcontext.Web.Lists.GetById(listId); spcontext.Load(splist, l => l.EffectiveBasePermissions); spcontext.ExecuteQuery(); canEdit = splist.EffectiveBasePermissions.Has(PermissionKind.EditListItems); } return(canEdit.Value); } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the InternalApi.SPListService.CanEdit() method for ApplicationId: {1}. The exception message is: {2}", ex.GetType(), listId, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } }
public List <SPPermissionsLevel> Levels(string webUrl) { var levels = new List <SPPermissionsLevel>(); try { using (var clientContext = new SPContext(webUrl, credentials.Get(webUrl))) { var web = clientContext.Web; clientContext.Load(web, w => w.RoleDefinitions.Include( rd => rd.Id, rd => rd.Name, rd => rd.Description)); clientContext.ExecuteQuery(); foreach (var rd in web.RoleDefinitions) { levels.Add(new SPPermissionsLevel(rd.Id, rd.Name) { Description = rd.Description }); } } } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.Levels() method for SPWebUrl: {1}. The exception message is: {2}", ex.GetType(), webUrl, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } return(levels); }
public List <Folder> List(string url, Guid libraryId, string folderPath) { var folderList = new List <Folder>(); try { using (var clientContext = new SPContext(url, credentials.Get(url))) { var list = clientContext.Web.Lists.GetById(libraryId); var rootFolder = list.RootFolder; clientContext.Load(rootFolder, f => f.ServerRelativeUrl); var spfolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); var folderCollection = clientContext.LoadQuery(SP.ClientObjectQueryableExtension.Include(spfolder.Folders, f => f.Name, f => f.ServerRelativeUrl, f => f.ItemCount)); clientContext.ExecuteQuery(); folderList.AddRange(from f in folderCollection where !IsHiddenFolder(f.ServerRelativeUrl, rootFolder.ServerRelativeUrl) select new Folder(f.Name, f.ServerRelativeUrl, f.ItemCount, libraryId)); } } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.List() method LibraryId: {1} ServerRelativeUrl: '{2}' SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), libraryId, folderPath, url, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } return(folderList); }
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 Folder Get(string url, Guid libraryId, string folderPath) { Folder folder; try { using (var clientContext = new SPContext(url, credentials.Get(url))) { var spfolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); clientContext.Load(spfolder, f => f.Name, f => f.ServerRelativeUrl, f => f.ItemCount); clientContext.ExecuteQuery(); folder = new Folder(spfolder.Name, spfolder.ServerRelativeUrl, spfolder.ItemCount, libraryId); } } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.Get() method LibraryId: {1} ServerRelativeUrl: '{2}' SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), libraryId, folderPath, url, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } return(folder); }
public List <User> List(IEnumerable <string> emails) { var userList = new List <User>(); SP.Web web = spcontext.Site.RootWeb; foreach (string camlQuery in CamlQueryBuilder(emails.ToArray(), userProfileBatchCapacity, syncSettings.SPUserEmailFieldName)) { SP.ListItemCollection spuserCollection = web.SiteUserInfoList.GetItems(new CamlQuery { ViewXml = camlQuery }); spcontext.Load(spuserCollection); spcontext.ExecuteQuery(); InitUserList(spuserCollection, userList); } return(userList); }
public int GetWSSId(string url, string label) { using (var context = new SPContext(url, credentials.Get(url))) { try { var taxonomyList = context.Web.Lists.GetByTitle("TaxonomyHiddenList"); var taxItems = taxonomyList.GetItems(CAMLQueryBuilder.GetItemByTitle(label, new[] { "Title", "ID" })); context.Load(taxItems); context.ExecuteQuery(); if (taxItems.Any()) { return(taxItems[0].Id); } } catch (Exception ex) { SPLog.UnKnownError(ex, ex.Message); } } return(-1); }
public Folder Delete(string url, Guid libraryId, string folderPath) { Folder folder; try { using (var clientContext = new SPContext(url, credentials.Get(url))) { SP.List splist = clientContext.Web.Lists.GetById(libraryId); SP.Folder rootFolder = splist.RootFolder; clientContext.Load(rootFolder, f => f.ServerRelativeUrl); SP.Folder spfolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); clientContext.Load(spfolder, f => f.Name, f => f.ServerRelativeUrl, f => f.ItemCount); clientContext.ExecuteQuery(); bool pathIsNotEmpty = !String.IsNullOrEmpty(spfolder.ServerRelativeUrl.Trim('/')); bool isARootFolder = rootFolder.ServerRelativeUrl.Trim('/').Equals(spfolder.ServerRelativeUrl.Trim('/')); bool hasParentFolder = pathIsNotEmpty && !isARootFolder; if (hasParentFolder && !IsHiddenFolder(spfolder.ServerRelativeUrl, rootFolder.ServerRelativeUrl)) { folder = new Folder(spfolder.Name, spfolder.ServerRelativeUrl, spfolder.ItemCount, libraryId); spfolder.DeleteObject(); clientContext.ExecuteQuery(); } else { throw new SPInternalException("Folder can not be removed."); } } } catch (SPInternalException) { throw; } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.Delete() method LibraryId: {1} ServerRelativeUrl: '{2}' in SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), libraryId, folderPath, url, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } return(folder); }
private static bool TestCredentials(string url, Authentication auth) { try { using (var spcontext = new SPContext(url, auth)) { spcontext.Load(spcontext.Web, w => w.Id); spcontext.Load(spcontext.Site, s => s.Id); spcontext.ExecuteQuery(); return(true); } } catch { return(false); } }
public SPList Create(int templateType, ListCreateQuery options) { Validate(options); SPList list; try { using (var clientContext = new SPContext(options.SPWebUrl, credentials.Get(options.SPWebUrl))) { var site = clientContext.Site; clientContext.Load(site, s => s.Id); var listCreationInformation = new ListCreationInformation { Title = options.Title, Description = options.Description, TemplateType = templateType }; var splist = clientContext.Web.Lists.Add(listCreationInformation); clientContext.Load(splist, InstanceQuery); clientContext.ExecuteQuery(); list = new SPList(splist, site.Id) { GroupId = options.GroupId }; Add(new ListBase(list.GroupId, list.Id, options.TypeId, options.SPWebUrl) { ApplicationKey = list.Title }); } } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the InternalApi.SPListService.Create() method. The exception message is: {1}", ex.GetType(), ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } return(list); }
private void LoadSubProviders(List <IntegrationProvider> collection, IntegrationProvider current) { using (var clientContext = new SPContext(current.SPSiteURL, current.Authentication)) { var site = clientContext.Site; clientContext.Load(site, s => s.Id, s => s.Url); var webs = clientContext.Web.Webs; clientContext.Load(webs, ws => ws.Include(w => w.Id, w => w.ServerRelativeUrl, w => w.Title, w => w.Webs)); try { clientContext.ExecuteQuery(); } catch (Exception) { return; } Parallel.ForEach(webs, new ParallelOptions { MaxDegreeOfParallelism = 5 }, website => { var provider = new IntegrationProvider { SPSiteURL = SPSite.MergeUrl(current.SPSiteURL, website.ServerRelativeUrl), TEGroupId = current.TEGroupId, TEGroupName = current.TEGroupName, Authentication = current.Authentication, SPSiteName = website.Title, SPWebID = website.Id, SPSiteID = site.Id }; collection.Add(provider); if (website.Webs.Count > 0) { LoadSubProviders(collection, provider); } }); } }
public List <Folder> UpToParent(string url, Guid libraryId, string folderPath) { var folderList = new List <Folder>(); try { using (var clientContext = new SPContext(url, credentials.Get(url))) { var list = clientContext.Web.Lists.GetById(libraryId); var rootFolder = list.RootFolder; clientContext.Load(rootFolder, f => f.Name, f => f.ServerRelativeUrl, f => f.ItemCount); clientContext.ExecuteQuery(); bool pathIsNotEmpty = !String.IsNullOrEmpty(folderPath.Trim('/')); bool isARootFolder = rootFolder.ServerRelativeUrl.Trim('/').Equals(folderPath.Trim('/')); bool hasParentFolder = pathIsNotEmpty && !isARootFolder; while (hasParentFolder) { var spfolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); clientContext.Load(spfolder, f => f.Name, f => f.ServerRelativeUrl, f => f.ItemCount); clientContext.Load(spfolder.ParentFolder, p => p.ServerRelativeUrl); clientContext.ExecuteQuery(); folderList.Add(new Folder(spfolder.Name, spfolder.ServerRelativeUrl, spfolder.ItemCount, libraryId)); folderPath = spfolder.ParentFolder.ServerRelativeUrl; pathIsNotEmpty = !String.IsNullOrEmpty(folderPath.Trim('/')); isARootFolder = rootFolder.ServerRelativeUrl.Trim('/').Equals(folderPath.Trim('/')); hasParentFolder = pathIsNotEmpty && !isARootFolder; } folderList.Add(new Folder(rootFolder.Name, rootFolder.ServerRelativeUrl, rootFolder.ItemCount, libraryId)); } } catch (Exception ex) { string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.UpToParent() method LibraryId: {1} ServerRelativeUrl: '{2}' SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), libraryId, folderPath, url, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } return(folderList); }
private Folder GetFolderIfExists(List list, string folder) { FolderCollection folders = list.RootFolder.Folders; SPContext.Load(folders, fl => fl.Include(ct => ct.Name).Where(ct => ct.Name == folder)); SPContext.ExecuteQuery(); return(folders.FirstOrDefault()); }
public Microsoft.SharePoint.Client.Field Get(string url, Guid listId, Guid fieldId) { using (var spcontext = new SPContext(url, credentials.Get(url))) { var list = spcontext.Web.Lists.GetById(listId); var field = list.Fields.GetById(fieldId); spcontext.Load(field); spcontext.ExecuteQuery(); return(field); } }
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())); } }