/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package file through a supporting persistence connector. /// </summary> /// <param name="packageFileName">The name of the .PNP package file. If the .PNP extension is missing, it will be added</param> /// <param name="persistenceConnector">The FileConnector object that will be used for physical persistence of the file</param> /// <param name="author">The Author of the .PNP package file, if any. Optional</param> /// <param name="signingCertificate">The X.509 certificate to use for digital signature of the template, optional</param> public OpenXMLConnector(string packageFileName, FileConnectorBase persistenceConnector, String author = null, X509Certificate2 signingCertificate = null) : base() { if (String.IsNullOrEmpty(packageFileName)) { throw new ArgumentException("packageFileName"); } else if (!packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // Check for file extension packageFileName = packageFileName.EndsWith(".") ? (packageFileName + "pnp") : (packageFileName + ".pnp"); } this.packageFileName = packageFileName; if (persistenceConnector == null) { throw new ArgumentException("persistenceConnector"); } this.persistenceConnector = persistenceConnector; // Try to load the .PNP package file var packageStream = persistenceConnector.GetFileStream(packageFileName); if (packageStream != null) { // If the .PNP package exists unpack it into PnP OpenXML package info object using (StreamReader sr = new StreamReader(packageStream)) { Byte[] buffer = new Byte[packageStream.Length]; // TODO: Handle large files with chunking packageStream.Read(buffer, 0, (Int32)packageStream.Length); this.pnpInfo = buffer.UnpackTemplate(); } } else { // Otherwsie initialize a fresh new PnP OpenXML package info object this.pnpInfo = new PnPInfo() { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnPCoreUtilities.PnPCoreVersionTag, Author = !String.IsNullOrEmpty(author) ? author : String.Empty, }, }; } }
/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package file through a supporting persistence connector. /// </summary> /// <param name="packageFileName">The name of the .PNP package file. If the .PNP extension is missing, it will be added</param> /// <param name="persistenceConnector">The FileConnector object that will be used for physical persistence of the file</param> /// <param name="author">The Author of the .PNP package file, if any. Optional</param> /// <param name="signingCertificate">The X.509 certificate to use for digital signature of the template, optional</param> public OpenXMLConnector(string packageFileName, FileConnectorBase persistenceConnector, String author = null, X509Certificate2 signingCertificate = null) : base() { if (String.IsNullOrEmpty(packageFileName)) { throw new ArgumentException("packageFileName"); } else if (!packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // Check for file extension packageFileName = packageFileName.EndsWith(".") ? (packageFileName + "pnp") : (packageFileName + ".pnp"); } this.packageFileName = packageFileName; if (persistenceConnector == null) { throw new ArgumentException("persistenceConnector"); } this.persistenceConnector = persistenceConnector; // Try to load the .PNP package file var packageStream = persistenceConnector.GetFileStream(packageFileName); if (packageStream != null) { // If the .PNP package exists unpack it into PnP OpenXML package info object using (StreamReader sr = new StreamReader(packageStream)) { Byte[] buffer = new Byte[packageStream.Length]; // TODO: Handle large files with chunking packageStream.Read(buffer, 0, (Int32)packageStream.Length); this.pnpInfo = buffer.UnpackTemplate(); } } else { // Otherwsie initialize a fresh new PnP OpenXML package info object this.pnpInfo = new PnPInfo() { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnPCoreUtilities.PnPCoreVersionTag, Author = !String.IsNullOrEmpty(author) ? author : String.Empty, }, }; } }
/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package file through a supporting persistence connector. /// </summary> /// <param name="packageFileName">The name of the .PNP package file. If the .PNP extension is missing, it will be added</param> /// <param name="persistenceConnector">The FileConnector object that will be used for physical persistence of the file</param> /// <param name="author">The Author of the .PNP package file, if any. Optional</param> /// <param name="signingCertificate">The X.509 certificate to use for digital signature of the template, optional</param> /// <param name="templateFileName">The name of the tempalte file, optional</param> public OpenXMLConnector(string packageFileName, FileConnectorBase persistenceConnector, String author = null, X509Certificate2 signingCertificate = null, String templateFileName = null) : base() { if (String.IsNullOrEmpty(packageFileName)) { throw new ArgumentException("packageFileName"); } if (!packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // Check for file extension packageFileName = packageFileName.TrimEnd('.') + ".pnp"; } this.packageFileName = packageFileName; if (persistenceConnector == null) { throw new ArgumentException("persistenceConnector"); } this.persistenceConnector = persistenceConnector; // Try to load the .PNP package file var packageStream = persistenceConnector.GetFileStream(packageFileName); if (packageStream != null) { // If the .PNP package exists unpack it into PnP OpenXML package info object MemoryStream ms = packageStream.ToMemoryStream(); this.pnpInfo = ms.UnpackTemplate(); } else { // Otherwsie initialize a fresh new PnP OpenXML package info object this.pnpInfo = new PnPInfo() { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnPCoreUtilities.PnPCoreVersionTag, Author = !String.IsNullOrEmpty(author) ? author : String.Empty, TemplateFileName = templateFileName ?? "" }, }; } }
/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package file through a supporting persistence connector. /// </summary> /// <param name="packageFileName">The name of the .PNP package file. If the .PNP extension is missing, it will be added</param> /// <param name="persistenceConnector">The FileConnector object that will be used for physical persistence of the file</param> /// <param name="author">The Author of the .PNP package file, if any. Optional</param> /// <param name="signingCertificate">The X.509 certificate to use for digital signature of the template, optional</param> public OpenXMLConnector(string packageFileName, FileConnectorBase persistenceConnector, String author = null, X509Certificate2 signingCertificate = null) : base() { if (String.IsNullOrEmpty(packageFileName)) { throw new ArgumentException("packageFileName"); } if (!packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // Check for file extension packageFileName = packageFileName.TrimEnd('.') + ".pnp"; } this.packageFileName = packageFileName; if (persistenceConnector == null) { throw new ArgumentException("persistenceConnector"); } this.persistenceConnector = persistenceConnector; // Try to load the .PNP package file var packageStream = persistenceConnector.GetFileStream(packageFileName); if (packageStream != null) { // If the .PNP package exists unpack it into PnP OpenXML package info object MemoryStream ms = packageStream.ToMemoryStream(); this.pnpInfo = ms.UnpackTemplate(); } else { // Otherwsie initialize a fresh new PnP OpenXML package info object this.pnpInfo = new PnPInfo() { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnPCoreUtilities.PnPCoreVersionTag, Author = !String.IsNullOrEmpty(author) ? author : String.Empty, }, }; } }
private static Microsoft.SharePoint.Client.ContentType CreateContentType(Web web, ContentType templateContentType, TokenParser parser, FileConnectorBase connector, List<Microsoft.SharePoint.Client.ContentType> existingCTs = null, List<Microsoft.SharePoint.Client.Field> existingFields = null) { var name = parser.ParseString(templateContentType.Name); var description = parser.ParseString(templateContentType.Description); var id = parser.ParseString(templateContentType.Id); var group = parser.ParseString(templateContentType.Group); var createdCT = web.CreateContentType(name, description, id, group); foreach (var fieldRef in templateContentType.FieldRefs) { var field = web.Fields.GetById(fieldRef.Id); web.AddFieldToContentType(createdCT, field, fieldRef.Required, fieldRef.Hidden); } createdCT.ReadOnly = templateContentType.ReadOnly; createdCT.Hidden = templateContentType.Hidden; createdCT.Sealed = templateContentType.Sealed; if (!string.IsNullOrEmpty(parser.ParseString(templateContentType.DocumentTemplate))) { createdCT.DocumentTemplate = parser.ParseString(templateContentType.DocumentTemplate); } if (!String.IsNullOrEmpty(templateContentType.NewFormUrl)) { createdCT.NewFormUrl = templateContentType.NewFormUrl; } if (!String.IsNullOrEmpty(templateContentType.EditFormUrl)) { createdCT.EditFormUrl = templateContentType.EditFormUrl; } if (!String.IsNullOrEmpty(templateContentType.DisplayFormUrl)) { createdCT.DisplayFormUrl = templateContentType.DisplayFormUrl; } // If the CT is a DocumentSet if (templateContentType.DocumentSetTemplate != null) { // Retrieve a reference to the DocumentSet Content Type Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate documentSetTemplate = Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate.GetDocumentSetTemplate(web.Context, createdCT); if (!String.IsNullOrEmpty(templateContentType.DocumentSetTemplate.WelcomePage)) { // TODO: Customize the WelcomePage of the DocumentSet } foreach (String ctId in templateContentType.DocumentSetTemplate.AllowedContentTypes) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == ctId); if (ct != null) { documentSetTemplate.AllowedContentTypes.Add(ct.Id); } } foreach (var doc in templateContentType.DocumentSetTemplate.DefaultDocuments) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == doc.ContentTypeId); if (ct != null) { using (Stream fileStream = connector.GetFileStream(doc.FileSourcePath)) { documentSetTemplate.DefaultDocuments.Add(doc.Name, ct.Id, ReadFullStream(fileStream)); } } } foreach (var sharedField in templateContentType.DocumentSetTemplate.SharedFields) { Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == sharedField); if (field != null) { documentSetTemplate.SharedFields.Add(field); } } foreach (var welcomePageField in templateContentType.DocumentSetTemplate.WelcomePageFields) { Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == welcomePageField); if (field != null) { documentSetTemplate.WelcomePageFields.Add(field); } } documentSetTemplate.Update(true); web.Context.ExecuteQueryRetry(); } web.Context.Load(createdCT); web.Context.ExecuteQueryRetry(); return createdCT; }
private static Microsoft.SharePoint.Client.ContentType CreateContentType(Web web, ContentType templateContentType, TokenParser parser, FileConnectorBase connector, List<Microsoft.SharePoint.Client.ContentType> existingCTs = null, List<Microsoft.SharePoint.Client.Field> existingFields = null) { var name = parser.ParseString(templateContentType.Name); var description = parser.ParseString(templateContentType.Description); var id = parser.ParseString(templateContentType.Id); var group = parser.ParseString(templateContentType.Group); var createdCT = web.CreateContentType(name, description, id, group); foreach (var fieldRef in templateContentType.FieldRefs) { var field = web.Fields.GetById(fieldRef.Id); web.AddFieldToContentType(createdCT, field, fieldRef.Required, fieldRef.Hidden); } // Add new CTs parser.AddToken(new ContentTypeIdToken(web, name, id)); #if !CLIENTSDKV15 // Set resources if (templateContentType.Name.ContainsResourceToken()) { createdCT.NameResource.SetUserResourceValue(templateContentType.Name, parser); } if(templateContentType.Description.ContainsResourceToken()) { createdCT.DescriptionResource.SetUserResourceValue(templateContentType.Description, parser); } #endif //Reorder the elements so that the new created Content Type has the same order as defined in the //template. The order can be different if the new Content Type inherits from another Content Type. //In this case the new Content Type has all field of the original Content Type and missing fields //will be added at the end. To fix this issue we ordering the fields once more. createdCT.FieldLinks.Reorder(templateContentType.FieldRefs.Select(fld => fld.Name).ToArray()); createdCT.ReadOnly = templateContentType.ReadOnly; createdCT.Hidden = templateContentType.Hidden; createdCT.Sealed = templateContentType.Sealed; if (!string.IsNullOrEmpty(parser.ParseString(templateContentType.DocumentTemplate))) { createdCT.DocumentTemplate = parser.ParseString(templateContentType.DocumentTemplate); } if (!String.IsNullOrEmpty(templateContentType.NewFormUrl)) { createdCT.NewFormUrl = templateContentType.NewFormUrl; } if (!String.IsNullOrEmpty(templateContentType.EditFormUrl)) { createdCT.EditFormUrl = templateContentType.EditFormUrl; } if (!String.IsNullOrEmpty(templateContentType.DisplayFormUrl)) { createdCT.DisplayFormUrl = templateContentType.DisplayFormUrl; } createdCT.Update(true); web.Context.ExecuteQueryRetry(); // If the CT is a DocumentSet if (templateContentType.DocumentSetTemplate != null) { // Retrieve a reference to the DocumentSet Content Type Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate documentSetTemplate = Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate.GetDocumentSetTemplate(web.Context, createdCT); if (!String.IsNullOrEmpty(templateContentType.DocumentSetTemplate.WelcomePage)) { // TODO: Customize the WelcomePage of the DocumentSet } foreach (String ctId in templateContentType.DocumentSetTemplate.AllowedContentTypes) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == ctId); if (ct != null) { documentSetTemplate.AllowedContentTypes.Add(ct.Id); } } foreach (var doc in templateContentType.DocumentSetTemplate.DefaultDocuments) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == doc.ContentTypeId); if (ct != null) { using (Stream fileStream = connector.GetFileStream(doc.FileSourcePath)) { documentSetTemplate.DefaultDocuments.Add(doc.Name, ct.Id, ReadFullStream(fileStream)); } } } foreach (var sharedField in templateContentType.DocumentSetTemplate.SharedFields) { Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == sharedField); if (field != null) { documentSetTemplate.SharedFields.Add(field); } } foreach (var welcomePageField in templateContentType.DocumentSetTemplate.WelcomePageFields) { Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == welcomePageField); if (field != null) { documentSetTemplate.WelcomePageFields.Add(field); } } documentSetTemplate.Update(true); web.Context.ExecuteQueryRetry(); } web.Context.Load(createdCT); web.Context.ExecuteQueryRetry(); return createdCT; }
private static Microsoft.SharePoint.Client.ContentType CreateContentType(Web web, ContentType templateContentType, TokenParser parser, FileConnectorBase connector, PnPMonitoredScope scope, List<Microsoft.SharePoint.Client.ContentType> existingCTs = null, List<Microsoft.SharePoint.Client.Field> existingFields = null, bool isNoScriptSite = false) { var name = parser.ParseString(templateContentType.Name); var description = parser.ParseString(templateContentType.Description); var id = parser.ParseString(templateContentType.Id); var group = parser.ParseString(templateContentType.Group); var createdCT = web.CreateContentType(name, description, id, group); foreach (var fieldRef in templateContentType.FieldRefs) { Microsoft.SharePoint.Client.Field field = null; try { // Try to get the field by ID field = web.Fields.GetById(fieldRef.Id); } catch (ArgumentException) { // In case of failure, if we have the name if (!String.IsNullOrEmpty(fieldRef.Name)) { // Let's try with that one field = web.Fields.GetByInternalNameOrTitle(fieldRef.Name); } } // Add it to the target content type // Notice that this code will fail if the field does not exist web.AddFieldToContentType(createdCT, field, fieldRef.Required, fieldRef.Hidden); } // Add new CTs parser.AddToken(new ContentTypeIdToken(web, name, id)); #if !ONPREMISES // Set resources if (templateContentType.Name.ContainsResourceToken()) { createdCT.NameResource.SetUserResourceValue(templateContentType.Name, parser); } if (templateContentType.Description.ContainsResourceToken()) { createdCT.DescriptionResource.SetUserResourceValue(templateContentType.Description, parser); } #endif //Reorder the elements so that the new created Content Type has the same order as defined in the //template. The order can be different if the new Content Type inherits from another Content Type. //In this case the new Content Type has all field of the original Content Type and missing fields //will be added at the end. To fix this issue we ordering the fields once more. createdCT.FieldLinks.Reorder(templateContentType.FieldRefs.Select(fld => parser.ParseString(fld.Name)).ToArray()); createdCT.ReadOnly = templateContentType.ReadOnly; createdCT.Hidden = templateContentType.Hidden; createdCT.Sealed = templateContentType.Sealed; if (templateContentType.DocumentSetTemplate == null) { // Only apply a document template when the contenttype is not a document set if (!string.IsNullOrEmpty(parser.ParseString(templateContentType.DocumentTemplate))) { createdCT.DocumentTemplate = parser.ParseString(templateContentType.DocumentTemplate); } } // Skipping updates of forms as we can't upload forms to noscript sites if (!isNoScriptSite) { if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.NewFormUrl))) { createdCT.NewFormUrl = parser.ParseString(templateContentType.NewFormUrl); } if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.EditFormUrl))) { createdCT.EditFormUrl = parser.ParseString(templateContentType.EditFormUrl); } if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.DisplayFormUrl))) { createdCT.DisplayFormUrl = parser.ParseString(templateContentType.DisplayFormUrl); } } else { if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.DisplayFormUrl)) || !String.IsNullOrEmpty(parser.ParseString(templateContentType.EditFormUrl)) || !String.IsNullOrEmpty(parser.ParseString(templateContentType.NewFormUrl))) { // log message scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ContentTypes_SkipCustomFormUrls, name); } } createdCT.Update(true); web.Context.ExecuteQueryRetry(); // If the CT is a DocumentSet if (templateContentType.DocumentSetTemplate != null) { // Retrieve a reference to the DocumentSet Content Type Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate documentSetTemplate = Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate.GetDocumentSetTemplate(web.Context, createdCT); // Load the collections to allow for deletion scenarions web.Context.Load(documentSetTemplate, d => d.AllowedContentTypes, d => d.DefaultDocuments, d => d.SharedFields, d => d.WelcomePageFields); web.Context.ExecuteQueryRetry(); if (!String.IsNullOrEmpty(templateContentType.DocumentSetTemplate.WelcomePage)) { // TODO: Customize the WelcomePage of the DocumentSet } // Add additional content types to the set of allowed content types bool hasDefaultDocumentContentTypeInTemplate = false; foreach (String ctId in templateContentType.DocumentSetTemplate.AllowedContentTypes) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == ctId); if (ct != null) { if (ct.Id.StringValue.Equals("0x0101", StringComparison.InvariantCultureIgnoreCase)) { hasDefaultDocumentContentTypeInTemplate = true; } documentSetTemplate.AllowedContentTypes.Add(ct.Id); } } // If the default document content type (0x0101) is not in our definition then remove it if (!hasDefaultDocumentContentTypeInTemplate) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == "0x0101"); if (ct != null) { documentSetTemplate.AllowedContentTypes.Remove(ct.Id); } } if (!isNoScriptSite) { foreach (var doc in templateContentType.DocumentSetTemplate.DefaultDocuments) { Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == doc.ContentTypeId); if (ct != null) { using (Stream fileStream = connector.GetFileStream(doc.FileSourcePath)) { documentSetTemplate.DefaultDocuments.Add(doc.Name, ct.Id, ReadFullStream(fileStream)); } } } } else { if (templateContentType.DocumentSetTemplate.DefaultDocuments.Any()) { scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ContentTypes_SkipDocumentSetDefaultDocuments, name); } } foreach (var sharedField in templateContentType.DocumentSetTemplate.SharedFields) { Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == sharedField); if (field != null) { documentSetTemplate.SharedFields.Add(field); } } foreach (var welcomePageField in templateContentType.DocumentSetTemplate.WelcomePageFields) { Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == welcomePageField); if (field != null) { documentSetTemplate.WelcomePageFields.Add(field); } } documentSetTemplate.Update(true); web.Context.ExecuteQueryRetry(); } web.Context.Load(createdCT); web.Context.ExecuteQueryRetry(); return createdCT; }