/// <summary> /// Generic uploader for the file to context web /// </summary> /// <param name="context"> /// The context /// </param> /// <param name="fullFilePath"> /// Full file name with path /// </param> /// <param name="folder"> /// Target folder /// </param> private void UploadFileToContextWeb(ClientContext context, string fullFilePath, Folder folder) { try { FileCreationInformation newFile = new FileCreationInformation(); newFile.Content = File.ReadAllBytes(fullFilePath); newFile.Url = folder.ServerRelativeUrl + "/" + Path.GetFileName(fullFilePath); newFile.Overwrite = true; Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile); context.Load(uploadFile); context.ExecuteQuery(); } catch (Exception ex) { // TODO - Proper logging on exceptions... this is not really acceptable string fuu = ex.ToString(); } }
private static void SetDocumentAsTemplate(ClientContext cc, Web web, string ctsId, string ctsfilename, string InternalName) { ContentType ct = web.ContentTypes.GetById(ctsId); cc.Load(ct); cc.ExecuteQuery(); // Get instance to the _cts folder created for the each of the content types string ctFolderServerRelativeURL = "_cts/" + InternalName; Folder ctFolder = web.GetFolderByServerRelativeUrl(ctFolderServerRelativeURL); cc.Load(ctFolder); cc.ExecuteQuery(); // Load the local template document string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ctsfilename); 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; newFile.Overwrite = true; Microsoft.SharePoint.Client.File uploadedFile = ctFolder.Files.Add(newFile); cc.Load(uploadedFile); cc.ExecuteQuery(); } ct.DocumentTemplate = fileName; ct.Update(true); cc.ExecuteQuery(); Log.Debug("Document template uploaded and set to the content type."); }
/// <summary> /// Add a web part to a site page. /// </summary> /// <param name="context"> /// The SharePoint context. /// </param> /// <param name="webpartxml"> /// The web part XML. /// </param> /// <param name="pageUrl"> /// The destination page URL. /// </param> /// <param name="zone"> /// The zone ID. /// </param> /// <param name="zoneIndex"> /// The zone index. /// </param> /// <param name="replacetag">Placeholder tag to replace on publishing pages.</param> public static void AddWebPart(ClientContext context, string webpartxml, string pageUrl, string zone, int zoneIndex, string replacetag) { Microsoft.SharePoint.Client.File page = null; try { page = context.Web.GetFileByServerRelativeUrl(pageUrl); ListItem listItemHome = page.ListItemAllFields; context.Load(page); context.Load(listItemHome); context.ExecuteQuery(); page.CheckOut(); LimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared); WebPartDefinition importedWebPart = wpm.ImportWebPart(webpartxml); WebPartDefinition webPart = wpm.AddWebPart(importedWebPart.WebPart, zone, zoneIndex); context.Load(webPart, w => w.Id); context.ExecuteQuery(); // Position the part at top of the Publishing Page if (zone == "wpz") { // SitePage item if (listItemHome.FieldValues.ContainsKey("WikiField")) { // Look for Placeholder text to replace, keeps the web part in the table (no zones) string pageContents = listItemHome["WikiField"] as string; if (pageContents != null && pageContents.Contains(replacetag)) { listItemHome["WikiField"] = pageContents.Replace(replacetag, GetEmbeddedWebPart(webPart.Id)); } else { listItemHome["WikiField"] = string.Concat(GetEmbeddedWebPart(webPart.Id), pageContents); } } else if (listItemHome.FieldValues.ContainsKey("PublishingPageContent")) { // Pages item listItemHome["PublishingPageContent"] = string.Concat(GetEmbeddedWebPart(webPart.Id), "<br/>", listItemHome["PublishingPageContent"], "<br/>"); } listItemHome.Update(); context.ExecuteQuery(); } } catch (Exception e) { // Error handling System.Diagnostics.Trace.TraceError("Error Adding Opportunity WebPart to page " + e.Message); } finally { if (page != null) { page.CheckIn("Added the Opportunity Details Web Part", CheckinType.MinorCheckIn); ////page.Publish("Added the Opportunity Details Web Part"); context.ExecuteQuery(); } } }