本文整理汇总了C#中Web.GetFolderByServerRelativeUrl方法的典型用法代码示例。如果您正苦于以下问题:C# Web.GetFolderByServerRelativeUrl方法的具体用法?C# Web.GetFolderByServerRelativeUrl怎么用?C# Web.GetFolderByServerRelativeUrl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Web
的用法示例。
在下文中一共展示了Web.GetFolderByServerRelativeUrl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddHtmlToWikiPage
public void AddHtmlToWikiPage(ClientContext ctx, Web web, string folder, string html, string page)
{
Microsoft.SharePoint.Client.Folder pagesLib = web.GetFolderByServerRelativeUrl(folder);
ctx.Load(pagesLib.Files);
ctx.ExecuteQuery();
Microsoft.SharePoint.Client.File wikiPage = null;
foreach (Microsoft.SharePoint.Client.File aspxFile in pagesLib.Files)
{
if (aspxFile.Name.Equals(page, StringComparison.InvariantCultureIgnoreCase))
{
wikiPage = aspxFile;
break;
}
}
if (wikiPage == null)
{
return;
}
ctx.Load(wikiPage);
ctx.Load(wikiPage.ListItemAllFields);
ctx.ExecuteQuery();
string wikiField = (string)wikiPage.ListItemAllFields["WikiField"];
Microsoft.SharePoint.Client.ListItem listItem = wikiPage.ListItemAllFields;
listItem["WikiField"] = html;
listItem.Update();
ctx.ExecuteQuery();
}
示例2: SetDocumentAsTemplate
private static void SetDocumentAsTemplate(ClientContext cc, Web web)
{
ContentType ct = web.ContentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB3");
cc.Load(ct);
cc.ExecuteQuery();
// Get instance to the _cts folder created for the each of the content types
string ctFolderServerRelativeURL = "_cts/" + ct.Name;
Folder ctFolder = web.GetFolderByServerRelativeUrl(ctFolderServerRelativeURL);
cc.Load(ctFolder);
cc.ExecuteQuery();
// Load the local template document
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "template.docx");
string fileName = System.IO.Path.GetFileName(path);
byte[] filecontent = System.IO.File.ReadAllBytes(path);
// Uplaod file to the Office365
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = filecontent;
newFile.Url = ctFolderServerRelativeURL + "/" + fileName;
Microsoft.SharePoint.Client.File uploadedFile = ctFolder.Files.Add(newFile);
cc.Load(uploadedFile);
cc.ExecuteQuery();
}
ct.DocumentTemplate = fileName;
ct.Update(true);
cc.ExecuteQuery();
Console.WriteLine("Document template uploaded and set to the content type.");
}
示例3: UploadFilesInFolder
public void UploadFilesInFolder(ClientContext context, Web web, ShContentFolder contentFolder)
{
Log.Info("Uploading files from contentfolder " + contentFolder.FolderName);
string uploadTargetFolder;
Folder rootFolder;
web.Lists.EnsureSiteAssetsLibrary();
context.Load(web.Lists);
context.ExecuteQuery();
if (!string.IsNullOrEmpty(contentFolder.ListUrl))
{
context.Load(web, w => w.ServerRelativeUrl);
context.ExecuteQuery();
var rootFolderServerRelativeUrl = Url.Combine(web.ServerRelativeUrl, contentFolder.ListUrl);
rootFolder = web.GetFolderByServerRelativeUrl(rootFolderServerRelativeUrl);
context.Load(rootFolder);
context.ExecuteQuery();
uploadTargetFolder = Url.Combine(rootFolderServerRelativeUrl, contentFolder.FolderUrl);
} else if (!string.IsNullOrEmpty(contentFolder.ListName))
{
var assetLibrary = web.Lists.GetByTitle(contentFolder.ListName);
context.Load(assetLibrary, l => l.Title, l => l.RootFolder);
context.ExecuteQuery();
rootFolder = assetLibrary.RootFolder;
uploadTargetFolder = Url.Combine(assetLibrary.RootFolder.ServerRelativeUrl, contentFolder.FolderUrl);
}
else
{
Log.ErrorFormat("You need to specify either ListName or ListUrl for the Content Folder {0}", contentFolder.FolderName);
return;
}
var configRootFolder = Path.Combine(_contentDirectoryPath, contentFolder.FolderName);
if (!web.DoesFolderExists(uploadTargetFolder))
{
web.Folders.Add(uploadTargetFolder);
}
context.ExecuteQuery();
foreach (string folder in Directory.GetDirectories(configRootFolder, "*", SearchOption.AllDirectories))
{
var folderName = Url.Combine(uploadTargetFolder, folder.Replace(configRootFolder, "").Replace("\\", "/"));
if (!web.DoesFolderExists(folderName))
{
web.Folders.Add(folderName);
}
}
context.ExecuteQuery();
List<ShFileProperties> filePropertiesCollection = null;
if (!string.IsNullOrEmpty(contentFolder.PropertiesFile))
{
var propertiesFilePath = Path.Combine(configRootFolder, contentFolder.PropertiesFile);
var filePersistanceProvider = new FilePersistanceProvider<List<ShFileProperties>>(propertiesFilePath);
filePropertiesCollection = filePersistanceProvider.Load();
}
context.Load(context.Site, site => site.ServerRelativeUrl);
context.Load(context.Web, w => w.ServerRelativeUrl, w => w.Language);
context.ExecuteQuery();
String[] excludedFileExtensions = { };
if (!string.IsNullOrEmpty(contentFolder.ExcludeExtensions))
{
excludedFileExtensions = contentFolder.ExcludeExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
var files = Directory.GetFiles(configRootFolder, "*", SearchOption.AllDirectories)
.Where(file => !excludedFileExtensions.Contains(Path.GetExtension(file).ToLower())).ToList();
if (IncrementalUpload)
{
files = files.Where(f =>!LastUpload.ContainsKey(contentFolder.FolderName) || new FileInfo(f).LastWriteTimeUtc > LastUpload[contentFolder.FolderName]).ToList();
}
int filesUploaded = 0;
foreach (string filePath in files)
{
var pathToFileFromRootFolder = filePath.Replace(configRootFolder.TrimEnd(new []{'\\'}) + "\\", "");
var fileName = Path.GetFileName(pathToFileFromRootFolder);
if (!string.IsNullOrEmpty(contentFolder.PropertiesFile) && contentFolder.PropertiesFile == fileName)
{
Log.DebugFormat("Skipping file upload of {0} since it's used as a configuration file", fileName);
continue;
}
Log.DebugFormat("Uploading file {0} to {1}", fileName, contentFolder.ListUrl);
var fileUrl = GetFileUrl(uploadTargetFolder, pathToFileFromRootFolder, filePropertiesCollection);
web.CheckOutFile(fileUrl);
var newFile = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(filePath),
Url = fileUrl,
Overwrite = true
};
//.........这里部分代码省略.........
示例4: AddHtmlToWikiPage
/// <summary>
/// Add web part to a wiki style page
/// </summary>
/// <param name="html">html to be inserted</param>
/// <param name="page">Page to add the web part on</param>
/// <param name="row">Row of the wiki table that should hold the inserted web part</param>
/// <param name="col">Column of the wiki table that should hold the inserted web part</param>
public void AddHtmlToWikiPage(ClientContext ctx, Web web, string folder, string html, string page, int row, int col)
{
//Note: getfilebyserverrelativeurl did not work...not sure why not
Microsoft.SharePoint.Client.Folder pagesLib = web.GetFolderByServerRelativeUrl(folder);
ctx.Load(pagesLib.Files);
ctx.ExecuteQuery();
Microsoft.SharePoint.Client.File webPartPage = null;
foreach (Microsoft.SharePoint.Client.File aspxFile in pagesLib.Files)
{
if (aspxFile.Name.Equals(page, StringComparison.InvariantCultureIgnoreCase))
{
webPartPage = aspxFile;
break;
}
}
if (webPartPage == null)
{
return;
}
ctx.Load(webPartPage);
ctx.Load(webPartPage.ListItemAllFields);
ctx.ExecuteQuery();
string wikiField = (string)webPartPage.ListItemAllFields["WikiField"];
XmlDocument xd = new XmlDocument();
xd.PreserveWhitespace = true;
xd.LoadXml(wikiField);
// Sometimes the wikifield content seems to be surrounded by an additional div?
XmlElement layoutsTable = xd.SelectSingleNode("div/div/table") as XmlElement;
if (layoutsTable == null)
{
layoutsTable = xd.SelectSingleNode("div/table") as XmlElement;
}
// Add the html content
XmlElement layoutsZoneInner = layoutsTable.SelectSingleNode(string.Format("tbody/tr[{0}]/td[{1}]/div/div", row, col)) as XmlElement;
XmlText text = xd.CreateTextNode("!!123456789!!");
layoutsZoneInner.AppendChild(text);
ListItem listItem = webPartPage.ListItemAllFields;
listItem["WikiField"] = xd.OuterXml.Replace("!!123456789!!", html);
listItem.Update();
ctx.ExecuteQuery();
}
示例5: AddWebPartToWikiPage
/// <summary>
/// Add web part to a wiki style page
/// </summary>
/// <param name="properties">Site to insert the web part on</param>
/// <param name="webPart">Information about the web part to insert</param>
/// <param name="page">Page to add the web part on</param>
/// <param name="row">Row of the wiki table that should hold the inserted web part</param>
/// <param name="col">Column of the wiki table that should hold the inserted web part</param>
/// <param name="addSpace">Does a blank line need to be added after the web part (to space web parts)</param>
public void AddWebPartToWikiPage(ClientContext ctx, Web web, string folder, WebPartEntity webPart, string page, int row, int col, bool addSpace)
{
//Note: getfilebyserverrelativeurl did not work...not sure why not
Microsoft.SharePoint.Client.Folder pagesLib = web.GetFolderByServerRelativeUrl(folder);
ctx.Load(pagesLib.Files);
ctx.ExecuteQuery();
Microsoft.SharePoint.Client.File webPartPage = null;
foreach (Microsoft.SharePoint.Client.File aspxFile in pagesLib.Files)
{
if (aspxFile.Name.Equals(page, StringComparison.InvariantCultureIgnoreCase))
{
webPartPage = aspxFile;
break;
}
}
if (webPartPage == null)
{
return;
}
ctx.Load(webPartPage);
ctx.Load(webPartPage.ListItemAllFields);
ctx.ExecuteQuery();
string wikiField = (string)webPartPage.ListItemAllFields["WikiField"];
LimitedWebPartManager limitedWebPartManager = webPartPage.GetLimitedWebPartManager(PersonalizationScope.Shared);
WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(webPart.WebPartXml);
WebPartDefinition wpdNew = limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, "wpz", 0);
ctx.Load(wpdNew);
ctx.ExecuteQuery();
//HTML structure in default team site home page (W16)
//<div class="ExternalClass284FC748CB4242F6808DE69314A7C981">
// <div class="ExternalClass5B1565E02FCA4F22A89640AC10DB16F3">
// <table id="layoutsTable" style="width:100%;">
// <tbody>
// <tr style="vertical-align:top;">
// <td colspan="2">
// <div class="ms-rte-layoutszone-outer" style="width:100%;">
// <div class="ms-rte-layoutszone-inner" style="word-wrap:break-word;margin:0px;border:0px;">
// <div><span><span><div class="ms-rtestate-read ms-rte-wpbox"><div class="ms-rtestate-read 9ed0c0ac-54d0-4460-9f1c-7e98655b0847" id="div_9ed0c0ac-54d0-4460-9f1c-7e98655b0847"></div><div class="ms-rtestate-read" id="vid_9ed0c0ac-54d0-4460-9f1c-7e98655b0847" style="display:none;"></div></div></span></span><p> </p></div>
// <div class="ms-rtestate-read ms-rte-wpbox">
// <div class="ms-rtestate-read c7a1f9a9-4e27-4aa3-878b-c8c6c87961c0" id="div_c7a1f9a9-4e27-4aa3-878b-c8c6c87961c0"></div>
// <div class="ms-rtestate-read" id="vid_c7a1f9a9-4e27-4aa3-878b-c8c6c87961c0" style="display:none;"></div>
// </div>
// </div>
// </div>
// </td>
// </tr>
// <tr style="vertical-align:top;">
// <td style="width:49.95%;">
// <div class="ms-rte-layoutszone-outer" style="width:100%;">
// <div class="ms-rte-layoutszone-inner" style="word-wrap:break-word;margin:0px;border:0px;">
// <div class="ms-rtestate-read ms-rte-wpbox">
// <div class="ms-rtestate-read b55b18a3-8a3b-453f-a714-7e8d803f4d30" id="div_b55b18a3-8a3b-453f-a714-7e8d803f4d30"></div>
// <div class="ms-rtestate-read" id="vid_b55b18a3-8a3b-453f-a714-7e8d803f4d30" style="display:none;"></div>
// </div>
// </div>
// </div>
// </td>
// <td class="ms-wiki-columnSpacing" style="width:49.95%;">
// <div class="ms-rte-layoutszone-outer" style="width:100%;">
// <div class="ms-rte-layoutszone-inner" style="word-wrap:break-word;margin:0px;border:0px;">
// <div class="ms-rtestate-read ms-rte-wpbox">
// <div class="ms-rtestate-read 0b2f12a4-3ab5-4a59-b2eb-275bbc617f95" id="div_0b2f12a4-3ab5-4a59-b2eb-275bbc617f95"></div>
// <div class="ms-rtestate-read" id="vid_0b2f12a4-3ab5-4a59-b2eb-275bbc617f95" style="display:none;"></div>
// </div>
// </div>
// </div>
// </td>
// </tr>
// </tbody>
// </table>
// <span id="layoutsData" style="display:none;">true,false,2</span>
// </div>
//</div>
XmlDocument xd = new XmlDocument();
xd.PreserveWhitespace = true;
xd.LoadXml(wikiField);
// Sometimes the wikifield content seems to be surrounded by an additional div?
XmlElement layoutsTable = xd.SelectSingleNode("div/div/table") as XmlElement;
if (layoutsTable == null)
{
layoutsTable = xd.SelectSingleNode("div/table") as XmlElement;
}
//.........这里部分代码省略.........
示例6: ExecuteObjectSearch
private object ExecuteObjectSearch(string serverRelativePath, Web web)
{
var ctx = web.Context;
File file;
Folder folder;
var scope = new ExceptionHandlingScope(ctx);
var tryFileFirst = serverRelativePath.Split(PathSeparator.ToCharArray()).Last().Contains(".");
if (tryFileFirst)
{
using (scope.StartScope())
{
using (scope.StartTry())
{
file = web.GetFileByServerRelativeUrl(serverRelativePath);
ctx.Load(file);
}
using (scope.StartCatch())
{
folder = web.GetFolderByServerRelativeUrl(serverRelativePath);
ctx.Load(folder);
}
}
}
else
{
using (scope.StartScope())
{
using (scope.StartTry())
{
folder = web.GetFolderByServerRelativeUrl(serverRelativePath);
ctx.Load(folder);
}
using (scope.StartCatch())
{
file = web.GetFileByServerRelativeUrl(serverRelativePath);
ctx.Load(file);
}
}
}
try
{
ctx.ExecuteQueryRetry();
}
catch (Exception e)
{
return e;
}
//Check if we got data
if (IsPropertyAvailable(file, "Name"))
{
return file;
}
else if (IsPropertyAvailable(folder, "Name"))
{
return folder;
}
return null;
}
示例7: DoesFolderExists
/// <summary>
/// Utility function to check if the folder name exists already in the context web
/// </summary>
/// <param name="clientContext"></param>
/// <param name="targetFolderUrl"></param>
/// <returns></returns>
private bool DoesFolderExists(ClientContext clientContext, Web web, string targetFolderUrl)
{
Folder folder = web.GetFolderByServerRelativeUrl(targetFolderUrl);
clientContext.Load(folder);
bool exists = false;
try
{
clientContext.ExecuteQuery();
exists = true;
}
catch (Exception ex)
{ }
return exists;
}
示例8: EnsureFolder
private static Folder EnsureFolder(Web web, string listUrl, string folderUrl, Folder parentFolder) {
Folder folder = null;
var folderServerRelativeUrl = parentFolder == null ? listUrl.TrimEnd(Program.trimChars) + "/" + folderUrl : parentFolder.ServerRelativeUrl.TrimEnd(Program.trimChars) + "/" + folderUrl;
if (string.IsNullOrEmpty(folderUrl)) {
return null;
}
var lists = web.Lists;
web.Context.Load(web);
web.Context.Load(lists, l => l.Include(ll => ll.DefaultViewUrl));
web.Context.ExecuteQuery();
ExceptionHandlingScope scope = new ExceptionHandlingScope(web.Context);
using (scope.StartScope()) {
using (scope.StartTry()) {
folder = web.GetFolderByServerRelativeUrl(folderServerRelativeUrl);
web.Context.Load(folder);
}
using (scope.StartCatch()) {
var list = lists.Where(l => l.DefaultViewUrl.IndexOf(listUrl, StringComparison.CurrentCultureIgnoreCase) >= 0).FirstOrDefault();
if (parentFolder == null) {
parentFolder = list.RootFolder;
}
folder = parentFolder.Folders.Add(folderUrl);
web.Context.Load(folder);
}
using (scope.StartFinally()) {
folder = web.GetFolderByServerRelativeUrl(folderServerRelativeUrl);
web.Context.Load(folder);
}
}
web.Context.ExecuteQuery();
return folder;
}
示例9: CreateList
private Tuple<List, TokenParser> CreateList(Web web, ListInstance list, TokenParser parser, PnPMonitoredScope scope)
{
var listCreate = new ListCreationInformation();
listCreate.Description = list.Description;
listCreate.TemplateType = list.TemplateType;
listCreate.Title = parser.ParseString(list.Title);
// the line of code below doesn't add the list to QuickLaunch
// the OnQuickLaunch property is re-set on the Created List object
listCreate.QuickLaunchOption = list.OnQuickLaunch ? QuickLaunchOptions.On : QuickLaunchOptions.Off;
listCreate.Url = parser.ParseString(list.Url);
listCreate.TemplateFeatureId = list.TemplateFeatureID;
var createdList = web.Lists.Add(listCreate);
createdList.Update();
web.Context.Load(createdList,
l => l.BaseTemplate,
l => l.RootFolder.ServerRelativeUrl,
l => l.Title,
l => l.ParentWebUrl,
l => l.Id,
l => l.Description,
l => l.OnQuickLaunch,
l => l.Hidden,
l => l.Fields.IncludeWithDefaultProperties(
f => f.SchemaXml
),
l => l.ContentTypesEnabled,
l => l.ContentTypes.IncludeWithDefaultProperties(
ct => ct.Id,
ct => ct.StringId,
ct => ct.Name),
l => l.EnableAttachments,
l => l.EnableFolderCreation,
l => l.EnableMinorVersions,
l => l.DraftVersionVisibility,
l => l.Views
#if !CLIENTSDKV15
, l => l.MajorWithMinorVersionsLimit
#endif
);
web.Context.ExecuteQueryRetry();
#if !CLIENTSDKV15
if (list.Title.ContainsResourceToken())
{
createdList.TitleResource.SetUserResourceValue(list.Title, parser);
}
if(list.Description.ContainsResourceToken())
{
createdList.DescriptionResource.SetUserResourceValue(list.Description, parser);
}
#endif
if (!String.IsNullOrEmpty(list.DocumentTemplate))
{
var documentTemplateUrl = parser.ParseString(list.DocumentTemplate);
var templateFolderUrl = documentTemplateUrl.Substring(0, documentTemplateUrl.LastIndexOf('/'));
var templateForder = web.GetFolderByServerRelativeUrl(templateFolderUrl);
var templateName = documentTemplateUrl.Substring(documentTemplateUrl.LastIndexOf('/') + 1);
var templateStream = new MemoryStream(Encoding.UTF8.GetBytes(list.DocumentTemplateContent));
templateForder.UploadFile(templateName, templateStream, true);
createdList.DocumentTemplateUrl = documentTemplateUrl;
}
// EnableAttachments are not supported for DocumentLibraries and Surveys
// TODO: the user should be warned
if (createdList.BaseTemplate != (int)ListTemplateType.DocumentLibrary && createdList.BaseTemplate != (int)ListTemplateType.Survey)
{
createdList.EnableAttachments = list.EnableAttachments;
}
createdList.EnableModeration = list.EnableModeration;
// Done for all other lists than for Survey - With Surveys versioning configuration will cause an exception
if (createdList.BaseTemplate != (int)ListTemplateType.Survey)
{
createdList.EnableVersioning = list.EnableVersioning;
if (list.EnableVersioning)
{
#if !CLIENTSDKV15
createdList.MajorVersionLimit = list.MaxVersionLimit;
#endif
if (createdList.BaseTemplate == (int)ListTemplateType.DocumentLibrary)
{
// Only supported on Document Libraries
createdList.EnableMinorVersions = list.EnableMinorVersions;
createdList.DraftVersionVisibility = (DraftVisibilityType)list.DraftVersionVisibility;
if (list.EnableMinorVersions)
{
createdList.MajorWithMinorVersionsLimit = list.MinorVersionLimit; // Set only if enabled, otherwise you'll get exception due setting value to zero.
// DraftVisibilityType.Approver is available only when the EnableModeration option of the list is true
if (DraftVisibilityType.Approver ==
(DraftVisibilityType)list.DraftVersionVisibility)
{
//.........这里部分代码省略.........
示例10: DeleteDemoPages
public void DeleteDemoPages(ClientContext cc, Web web, string folder)
{
//Note: getfilebyserverrelativeurl did not work...not sure why not
Microsoft.SharePoint.Client.Folder pagesLib = web.GetFolderByServerRelativeUrl(folder);
cc.Load(pagesLib.Files);
cc.ExecuteQuery();
List<File> toDelete = new List<File>();
foreach (Microsoft.SharePoint.Client.File aspxFile in pagesLib.Files)
{
if (aspxFile.Name.StartsWith("scenario1", StringComparison.InvariantCultureIgnoreCase))
{
toDelete.Add(aspxFile);
}
}
for (int i = 0; i < toDelete.Count; i++)
{
toDelete[i].DeleteObject();
}
cc.ExecuteQuery();
}
示例11: EnsureFolder
private static void EnsureFolder(Web web, string filePath, string fileFolder)
{
if (string.IsNullOrEmpty(fileFolder))
{
return;
}
var lists = web.Lists;
web.Context.Load(web);
web.Context.Load(lists, l => l.Include(ll => ll.DefaultViewUrl));
web.Context.ExecuteQuery();
ExceptionHandlingScope scope = new ExceptionHandlingScope(web.Context);
using (scope.StartScope())
{
using (scope.StartTry())
{
var folder = web.GetFolderByServerRelativeUrl(string.Concat(filePath, fileFolder));
web.Context.Load(folder);
}
using (scope.StartCatch())
{
var list = lists.Where(l => l.DefaultViewUrl.IndexOf(filePath, StringComparison.CurrentCultureIgnoreCase) >= 0).FirstOrDefault();
ListItemCreationInformation newFolder = new ListItemCreationInformation();
newFolder.UnderlyingObjectType = FileSystemObjectType.Folder;
newFolder.FolderUrl = filePath.TrimEnd(Program.trimChars);
newFolder.LeafName = fileFolder;
ListItem item = list.AddItem(newFolder);
web.Context.Load(item);
item.Update();
}
using (scope.StartFinally())
{
var folder = web.GetFolderByServerRelativeUrl(string.Concat(filePath, fileFolder));
web.Context.Load(folder);
}
}
web.Context.ExecuteQuery();
}
示例12: GetFolder
internal Folder GetFolder(Web web)
{
Folder folder = null;
if (Folder != null)
{
folder = Folder;
}
#if !SP2013
else if (Id != Guid.Empty)
{
folder = web.GetFolderById(Id);
}
#endif
else if (!string.IsNullOrEmpty(ServerRelativeUrl))
{
folder = web.GetFolderByServerRelativeUrl(ServerRelativeUrl);
}
if (folder != null)
{
web.Context.Load(folder);
web.Context.ExecuteQueryRetry();
}
return folder;
}
示例13: LoadCurrentList
private static List LoadCurrentList(Web web, ListDefinition listModel)
{
var context = web.Context;
List currentList = null;
#pragma warning disable 618
var listUrl = UrlUtility.CombineUrl(web.ServerRelativeUrl, listModel.GetListUrl());
#pragma warning restore 618
Folder folder;
var scope = new ExceptionHandlingScope(context);
using (scope.StartScope())
{
using (scope.StartTry())
{
folder = web.GetFolderByServerRelativeUrl(listUrl);
context.Load(folder);
}
using (scope.StartCatch())
{
}
}
context.ExecuteQueryWithTrace();
if (!scope.HasException && folder != null && folder.ServerObjectIsNull != true)
{
#if !NET35
folder = web.GetFolderByServerRelativeUrl(listUrl);
context.Load(folder.Properties);
context.ExecuteQueryWithTrace();
var listId = new Guid(folder.Properties["vti_listname"].ToString());
var list = web.Lists.GetById(listId);
context.Load(list);
if (listModel.IndexedRootFolderPropertyKeys.Any())
{
context.Load(list, l => l.RootFolder.Properties);
}
context.ExecuteQueryWithTrace();
currentList = list;
#endif
#if NET35
// SP2010 CSOM hack
// http://impl.com/questions/4284722/sharepoint-2010-client-object-model-get-a-list-item-from-a-url
var listQuery = from list in web.Lists
where list.RootFolder.ServerRelativeUrl == listUrl
select list;
var queryResult = context.LoadQuery(listQuery);
context.ExecuteQueryWithTrace();
var resultList = queryResult.FirstOrDefault();
currentList = resultList;
#endif
}
return currentList;
}
示例14: UploadDisplayTemplateFileJS
public static void UploadDisplayTemplateFileJS(Web web, string dtFileName, string localFilePath, string displayTemplateFolderName, string title, string description)
{
try
{
Logger.LogInfoMessage(String.Format("Uploading Display Template JS File: {0} ...", dtFileName), false);
List mpGallery = web.GetCatalog((int)ListTemplateType.MasterPageCatalog);
Folder mpGalleryRoot = mpGallery.RootFolder;
web.Context.Load(mpGallery);
web.Context.Load(mpGalleryRoot);
web.Context.ExecuteQuery();
string serverRelativeFolderUrl = mpGalleryRoot.ServerRelativeUrl + "/Display Templates/" + displayTemplateFolderName;
Folder dtFolder = web.GetFolderByServerRelativeUrl(serverRelativeFolderUrl);
web.Context.Load(dtFolder);
web.Context.ExecuteQuery();
string dtFilePath = dtFolder.ServerRelativeUrl + "/" + dtFileName;
File dtFile = GetFileFromWeb(web, dtFilePath);
if (dtFile == null)
{
// Get the file name from the provided path
Byte[] fileBytes = System.IO.File.ReadAllBytes(localFilePath);
// Use CSOM to upload the file in
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = fileBytes;
newFile.Overwrite = true;
newFile.Url = dtFilePath;
File uploadFile = mpGalleryRoot.Files.Add(newFile);
web.Context.Load(uploadFile);
web.Context.ExecuteQuery();
}
// Grab the file we just uploaded so we can edit its properties
dtFile = GetFileFromWeb(web, dtFilePath);
if (mpGallery.ForceCheckout || mpGallery.EnableVersioning)
{
if (dtFile.CheckOutType == CheckOutType.None)
{
dtFile.CheckOut();
}
}
ListItem fileListItem = dtFile.ListItemAllFields;
string controlContentTypeId = GetContentType(web, mpGallery, "Display Template Code");
fileListItem["Title"] = title;
fileListItem["MasterPageDescription"] = description;
fileListItem["ContentTypeId"] = controlContentTypeId;
fileListItem["TargetControlType"] = ";#Content Web Parts;#";
fileListItem["DisplayTemplateLevel"] = "Item";
fileListItem["TemplateHidden"] = "0";
fileListItem["UIVersion"] = "15";
fileListItem["ManagedPropertyMapping"] = "'Link URL'{Link URL}:'Path','Line 1'{Line 1}:'Title','Line 2'{Line 2}:'','FileExtension','SecondaryFileExtension'";
fileListItem.Update();
if (mpGallery.ForceCheckout || mpGallery.EnableVersioning)
{
dtFile.CheckIn(string.Empty, CheckinType.MajorCheckIn);
if (mpGallery.EnableModeration)
{
dtFile.Approve("");
}
}
web.Context.ExecuteQuery();
Logger.LogSuccessMessage(String.Format("Uploaded Display Template JS File: {0}", dtFile.ServerRelativeUrl), false);
}
catch (Exception ex)
{
Logger.LogErrorMessage(String.Format("UploadDisplayTemplateFileJS() failed for {0}: Error={1}", web.Url, ex.Message), false);
}
}
示例15: LoadCurrentList
private static List LoadCurrentList(Web web, ListDefinition listModel)
{
var context = web.Context;
List currentList = null;
var listUrl = UrlUtility.CombineUrl(web.ServerRelativeUrl, listModel.GetListUrl());
Folder folder = null;
var scope = new ExceptionHandlingScope(context);
using (scope.StartScope())
{
using (scope.StartTry())
{
folder = web.GetFolderByServerRelativeUrl(listUrl);
context.Load(folder);
}
using (scope.StartCatch())
{
}
}
context.ExecuteQueryWithTrace();
if (!scope.HasException && folder != null && folder.ServerObjectIsNull != true)
{
folder = web.GetFolderByServerRelativeUrl(listUrl);
context.Load(folder.Properties);
context.ExecuteQueryWithTrace();
var listId = new Guid(folder.Properties["vti_listname"].ToString());
var list = web.Lists.GetById(listId);
context.Load(list);
context.ExecuteQueryWithTrace();
currentList = list;
}
return currentList;
}