Пример #1
0
        public ProvisioningTemplate ToProvisioningTemplate(Stream template, String identifier)
        {
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            // Crate a copy of the source stream
            MemoryStream sourceStream = new MemoryStream();

            template.CopyTo(sourceStream);
            sourceStream.Position = 0;

            // Check the provided template against the XML schema
            if (!this.IsValid(sourceStream))
            {
                // TODO: Use resource file
                throw new ApplicationException("The provided template is not valid!");
            }

            sourceStream.Position = 0;
            XDocument xml = XDocument.Load(sourceStream);

            V201503.SharePointProvisioningTemplate source = XMLSerializer.Deserialize <V201503.SharePointProvisioningTemplate>(xml);

            ProvisioningTemplate result = new ProvisioningTemplate();

            // Translate basic properties
            result.Id         = source.ID;
            result.Version    = Double.Parse(!String.IsNullOrEmpty(source.Version) ? source.Version : "0", new CultureInfo("en-US"));
            result.SitePolicy = source.SitePolicy;

            // Translate PropertyBagEntries, if any
            if (source.PropertyBagEntries != null)
            {
                result.PropertyBagEntries.AddRange(
                    from bag in source.PropertyBagEntries
                    select new Model.PropertyBagEntry
                {
                    Key   = bag.Key,
                    Value = bag.Value,
                });
            }

            // Translate Security configuration, if any
            if (source.Security != null)
            {
                if (source.Security.AdditionalAdministrators != null)
                {
                    result.Security.AdditionalAdministrators.AddRange(
                        from user in source.Security.AdditionalAdministrators
                        select new Model.User
                    {
                        Name = user.Name,
                    });
                }
                if (source.Security.AdditionalOwners != null)
                {
                    result.Security.AdditionalOwners.AddRange(
                        from user in source.Security.AdditionalOwners
                        select new Model.User
                    {
                        Name = user.Name,
                    });
                }
                if (source.Security.AdditionalMembers != null)
                {
                    result.Security.AdditionalMembers.AddRange(
                        from user in source.Security.AdditionalMembers
                        select new Model.User
                    {
                        Name = user.Name,
                    });
                }
                if (source.Security.AdditionalVisitors != null)
                {
                    result.Security.AdditionalVisitors.AddRange(
                        from user in source.Security.AdditionalVisitors
                        select new Model.User
                    {
                        Name = user.Name,
                    });
                }
            }

            // Translate Site Columns (Fields), if any
            if ((source.SiteFields != null) && (source.SiteFields.Any != null))
            {
                result.SiteFields.AddRange(
                    from field in source.SiteFields.Any
                    select new Field
                {
                    SchemaXml = field.OuterXml,
                });
            }

            // Translate ContentTypes, if any
            if ((source.ContentTypes != null) && (source.ContentTypes.Any != null))
            {
                // Convert the SchemaXml property to the new format
                foreach (var contentType in source.ContentTypes.Any)
                {
                    var xelement = XElement.Parse(contentType.OuterXml);
                    var ID       = xelement.Attribute("ID") != null?xelement.Attribute("ID").Value : null;

                    var Name = xelement.Attribute("Name") != null?xelement.Attribute("Name").Value : null;

                    var Description = xelement.Attribute("Description") != null?xelement.Attribute("Description").Value : null;

                    var Group = xelement.Attribute("Group") != null?xelement.Attribute("Group").Value : null;

                    var Hidden = xelement.Attribute("Hidden") != null?bool.Parse(xelement.Attribute("Hidden").Value) : false;

                    var ReadOnly = xelement.Attribute("ReadOnly") != null?bool.Parse(xelement.Attribute("ReadOnly").Value) : false;

                    var Sealed = xelement.Attribute("Sealed") != null?bool.Parse(xelement.Attribute("Sealed").Value) : false;

                    var Overwrite = xelement.Attribute("Overwrite") != null?bool.Parse(xelement.Attribute("Overwrite").Value) : false;

                    var ct = new ContentType();
                    ct.Id          = ID;
                    ct.Name        = Name;
                    ct.Description = Description;
                    ct.Group       = Group;
                    ct.Hidden      = Hidden;
                    ct.ReadOnly    = ReadOnly;
                    ct.Sealed      = Sealed;
                    ct.Overwrite   = Overwrite;

                    var documentTemplateElement = xelement.Descendants("DocumentTemplate").FirstOrDefault();
                    if (documentTemplateElement != null)
                    {
                        ct.DocumentTemplate = documentTemplateElement.Attribute("TargetName") != null?documentTemplateElement.Attribute("TargetName").Value : null;
                    }
                    var fieldRefs = xelement.Descendants("FieldRefs").Descendants("FieldRef");
                    foreach (var fieldRef in fieldRefs)
                    {
                        FieldRef fr = new FieldRef();
                        fr.Id = fieldRef.Attribute("ID") != null?Guid.Parse(fieldRef.Attribute("ID").Value) : Guid.Empty;

                        fr.Required = fieldRef.Attribute("Required") != null?bool.Parse(fieldRef.Attribute("Required").Value) : false;

                        fr.Hidden = fieldRef.Attribute("Hidden") != null?bool.Parse(fieldRef.Attribute("Hidden").Value) : false;

                        ct.FieldRefs.Add(fr);
                    }
                    result.ContentTypes.Add(ct);
                }
            }

            // Translate Lists Instances, if any
            if (source.Lists != null)
            {
                result.Lists.AddRange(
                    from list in source.Lists
                    select new Model.ListInstance(
                        (list.ContentTypeBindings != null ?
                         (from contentTypeBinding in list.ContentTypeBindings
                          select new Model.ContentTypeBinding
                {
                    ContentTypeId = contentTypeBinding.ContentTypeID,
                    Default = contentTypeBinding.Default,
                }) : null),
                        (list.Views != null ?
                         (from view in list.Views.Any
                          select new View
                {
                    SchemaXml = view.OuterXml,
                }) : null),
                        (list.Fields != null ?
                         (from field in list.Fields.Any
                          select new Field
                {
                    SchemaXml = field.OuterXml,
                }) : null),
                        (list.FieldRefs != null ?
                         (from fieldRef in list.FieldRefs
                          select new Model.FieldRef
                {
                    Id = Guid.Parse(fieldRef.ID)
                }) : null),
                        null
                        )
                {
                    ContentTypesEnabled = list.ContentTypesEnabled,
                    Description         = list.Description,
                    DocumentTemplate    = list.DocumentTemplate,
                    EnableVersioning    = list.EnableVersioning,
                    Hidden                     = list.Hidden,
                    MinorVersionLimit          = list.MinorVersionLimit,
                    MaxVersionLimit            = list.MaxVersionLimit,
                    OnQuickLaunch              = list.OnQuickLaunch,
                    RemoveExistingContentTypes = list.RemoveDefaultContentType,
                    TemplateType               = list.TemplateType,
                    Title = list.Title,
                    Url   = list.Url,
                });
            }

            // Translate Features, if any
            if (source.Features != null)
            {
                if (result.Features.SiteFeatures != null && source.Features.SiteFeatures != null)
                {
                    result.Features.SiteFeatures.AddRange(
                        from feature in source.Features.SiteFeatures
                        select new Model.Feature
                    {
                        Id         = new Guid(feature.ID),
                        Deactivate = feature.Deactivate,
                    });
                }
                if (result.Features.WebFeatures != null && source.Features.WebFeatures != null)
                {
                    result.Features.WebFeatures.AddRange(
                        from feature in source.Features.WebFeatures
                        select new Model.Feature
                    {
                        Id         = new Guid(feature.ID),
                        Deactivate = feature.Deactivate,
                    });
                }
            }

            // Translate CustomActions, if any
            if (source.CustomActions != null)
            {
                if (result.CustomActions.SiteCustomActions != null && source.CustomActions.SiteCustomActions != null)
                {
                    result.CustomActions.SiteCustomActions.AddRange(
                        from customAction in source.CustomActions.SiteCustomActions
                        select new Model.CustomAction
                    {
                        Description = customAction.Description,
                        Enabled     = customAction.Enabled,
                        Group       = customAction.Group,
                        ImageUrl    = customAction.ImageUrl,
                        Location    = customAction.Location,
                        Name        = customAction.Name,
                        RightsValue = customAction.RightsSpecified ? customAction.Rights : 0,
                        ScriptBlock = customAction.ScriptBlock,
                        ScriptSrc   = customAction.ScriptSrc,
                        Sequence    = customAction.SequenceSpecified ? customAction.Sequence : 100,
                        Title       = customAction.Title,
                        Url         = customAction.Url,
                    });
                }
                if (result.CustomActions.WebCustomActions != null && source.CustomActions.WebCustomActions != null)
                {
                    result.CustomActions.WebCustomActions.AddRange(
                        from customAction in source.CustomActions.WebCustomActions
                        select new Model.CustomAction
                    {
                        Description = customAction.Description,
                        Enabled     = customAction.Enabled,
                        Group       = customAction.Group,
                        ImageUrl    = customAction.ImageUrl,
                        Location    = customAction.Location,
                        Name        = customAction.Name,
                        RightsValue = customAction.RightsSpecified ? customAction.Rights : 0,
                        ScriptBlock = customAction.ScriptBlock,
                        ScriptSrc   = customAction.ScriptSrc,
                        Sequence    = customAction.SequenceSpecified ? customAction.Sequence : 100,
                        Title       = customAction.Title,
                        Url         = customAction.Url,
                    });
                }
            }

            // Translate Files, if any
            if (source.Files != null)
            {
                result.Files.AddRange(
                    from file in source.Files
                    select new Model.File
                {
                    Overwrite = file.Overwrite,
                    Src       = file.Src,
                    Folder    = file.Folder,
                });
            }

            // Translate ComposedLook, if any
            if (source.ComposedLook != null)
            {
                result.ComposedLook.AlternateCSS   = source.ComposedLook.AlternateCSS;
                result.ComposedLook.BackgroundFile = source.ComposedLook.BackgroundFile;
                result.ComposedLook.ColorFile      = source.ComposedLook.ColorFile;
                result.ComposedLook.FontFile       = source.ComposedLook.FontFile;
                result.ComposedLook.MasterPage     = source.ComposedLook.MasterPage;
                result.ComposedLook.Name           = source.ComposedLook.Name;
                result.ComposedLook.SiteLogo       = source.ComposedLook.SiteLogo;
                result.ComposedLook.Version        = source.ComposedLook.Version;
            }

            // Translate Providers, if any
            if (source.Providers != null)
            {
                result.ExtensibilityHandlers.AddRange(
                    from provider in source.Providers
                    select new Model.ExtensibilityHandler
                {
                    Assembly      = provider.Assembly,
                    Configuration = provider.Configuration != null ? provider.Configuration.ToProviderConfiguration() : null,
                    Enabled       = provider.Enabled,
                    Type          = provider.Type,
                });
            }

            return(result);
        }
        public ProvisioningTemplate ToProvisioningTemplate(Stream template, String identifier)
        {
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            // Crate a copy of the source stream
            MemoryStream sourceStream = new MemoryStream();
            template.CopyTo(sourceStream);
            sourceStream.Position = 0;

            // Check the provided template against the XML schema
            if (!this.IsValid(sourceStream))
            {
                // TODO: Use resource file
                throw new ApplicationException("The provided template is not valid!");
            }

            sourceStream.Position = 0;
            XDocument xml = XDocument.Load(sourceStream);
            V201503.SharePointProvisioningTemplate source = XMLSerializer.Deserialize<V201503.SharePointProvisioningTemplate>(xml);

            ProvisioningTemplate result = new ProvisioningTemplate();

            // Translate basic properties
            result.Id = source.ID;
            result.Version = Double.Parse(!String.IsNullOrEmpty(source.Version) ? source.Version : "0", new CultureInfo("en-US"));
            result.SitePolicy = source.SitePolicy;

            // Translate PropertyBagEntries, if any
            if (source.PropertyBagEntries != null)
            {
                result.PropertyBagEntries.AddRange(
                    from bag in source.PropertyBagEntries
                    select new Model.PropertyBagEntry
                    {
                        Key = bag.Key,
                        Value = bag.Value,
                    });
            }

            // Translate Security configuration, if any
            if (source.Security != null)
            {
                if (source.Security.AdditionalAdministrators != null)
                {
                    result.Security.AdditionalAdministrators.AddRange(
                    from user in source.Security.AdditionalAdministrators
                    select new Model.User
                    {
                        Name = user.Name,
                    });
                }
                if (source.Security.AdditionalOwners != null)
                {
                    result.Security.AdditionalOwners.AddRange(
                    from user in source.Security.AdditionalOwners
                    select new Model.User
                    {
                        Name = user.Name,
                    });
                }
                if (source.Security.AdditionalMembers != null)
                {
                    result.Security.AdditionalMembers.AddRange(
                    from user in source.Security.AdditionalMembers
                    select new Model.User
                    {
                        Name = user.Name,
                    });
                }
                if (source.Security.AdditionalVisitors != null)
                {
                    result.Security.AdditionalVisitors.AddRange(
                    from user in source.Security.AdditionalVisitors
                    select new Model.User
                    {
                        Name = user.Name,
                    });
                }
            }

            // Translate Site Columns (Fields), if any
            if ((source.SiteFields != null) && (source.SiteFields.Any != null))
            {
                result.SiteFields.AddRange(
                    from field in source.SiteFields.Any
                    select new Field
                    {
                        SchemaXml = field.OuterXml,
                    });
            }

            // Translate ContentTypes, if any
            if ((source.ContentTypes != null) && (source.ContentTypes.Any != null))
            {
                // Convert the SchemaXml property to the new format
                foreach (var contentType in source.ContentTypes.Any)
                {
                    var xelement = XElement.Parse(contentType.OuterXml);
                    var ID = xelement.Attribute("ID") != null ? xelement.Attribute("ID").Value : null;
                    var Name = xelement.Attribute("Name") != null ? xelement.Attribute("Name").Value : null;
                    var Description = xelement.Attribute("Description") != null ? xelement.Attribute("Description").Value : null;
                    var Group = xelement.Attribute("Group") != null ? xelement.Attribute("Group").Value : null;
                    var Hidden = xelement.Attribute("Hidden") != null ? bool.Parse(xelement.Attribute("Hidden").Value) : false;
                    var ReadOnly = xelement.Attribute("ReadOnly") != null ? bool.Parse(xelement.Attribute("ReadOnly").Value) : false;
                    var Sealed = xelement.Attribute("Sealed") != null ? bool.Parse(xelement.Attribute("Sealed").Value) : false;
                    var Overwrite = xelement.Attribute("Overwrite") != null ? bool.Parse(xelement.Attribute("Overwrite").Value) : false;

                    var ct = new ContentType();
                    ct.Id = ID;
                    ct.Name = Name;
                    ct.Description = Description;
                    ct.Group = Group;
                    ct.Hidden = Hidden;
                    ct.ReadOnly = ReadOnly;
                    ct.Sealed = Sealed;
                    ct.Overwrite = Overwrite;

                    var documentTemplateElement = xelement.Descendants("DocumentTemplate").FirstOrDefault();
                    if (documentTemplateElement != null)
                    {
                        ct.DocumentTemplate = documentTemplateElement.Attribute("TargetName") != null ? documentTemplateElement.Attribute("TargetName").Value : null;
                    }
                    var fieldRefs = xelement.Descendants("FieldRefs").Descendants("FieldRef");
                    foreach (var fieldRef in fieldRefs)
                    {
                        FieldRef fr = new FieldRef();
                        fr.Id = fieldRef.Attribute("ID") != null ? Guid.Parse(fieldRef.Attribute("ID").Value) : Guid.Empty;
                        fr.Required = fieldRef.Attribute("Required") != null ? bool.Parse(fieldRef.Attribute("Required").Value) : false;
                        fr.Hidden = fieldRef.Attribute("Hidden") != null ? bool.Parse(fieldRef.Attribute("Hidden").Value) : false;
                        ct.FieldRefs.Add(fr);
                    }
                    result.ContentTypes.Add(ct);

                }
            }

            // Translate Lists Instances, if any
            if (source.Lists != null)
            {
                result.Lists.AddRange(
                    from list in source.Lists
                    select new Model.ListInstance(
                        (list.ContentTypeBindings != null ?
                                (from contentTypeBinding in list.ContentTypeBindings
                                 select new Model.ContentTypeBinding
                                 {
                                     ContentTypeId = contentTypeBinding.ContentTypeID,
                                     Default = contentTypeBinding.Default,
                                 }) : null),
                        (list.Views != null ?
                                (from view in list.Views.Any
                                 select new View
                                 {
                                     SchemaXml = view.OuterXml,
                                 }) : null),
                        (list.Fields != null ?
                                (from field in list.Fields.Any
                                 select new Field
                                 {
                                     SchemaXml = field.OuterXml,
                                 }) : null),
                        (list.FieldRefs != null ?
                                 (from fieldRef in list.FieldRefs
                                  select new Model.FieldRef
                                  {
                                      Id = Guid.Parse(fieldRef.ID)
                                  }) : null),
                                  null
                         )
                    {
                        ContentTypesEnabled = list.ContentTypesEnabled,
                        Description = list.Description,
                        DocumentTemplate = list.DocumentTemplate,
                        EnableVersioning = list.EnableVersioning,
                        Hidden = list.Hidden,
                        MinorVersionLimit = list.MinorVersionLimit,
                        MaxVersionLimit = list.MaxVersionLimit,
                        OnQuickLaunch = list.OnQuickLaunch,
                        RemoveExistingContentTypes = list.RemoveDefaultContentType,
                        TemplateType = list.TemplateType,
                        Title = list.Title,
                        Url = list.Url,
                    });
            }

            // Translate Features, if any
            if (source.Features != null)
            {
                if (result.Features.SiteFeatures != null && source.Features.SiteFeatures != null)
                {
                    result.Features.SiteFeatures.AddRange(
                        from feature in source.Features.SiteFeatures
                        select new Model.Feature
                        {
                            Id = new Guid(feature.ID),
                            Deactivate = feature.Deactivate,
                        });
                }
                if (result.Features.WebFeatures != null && source.Features.WebFeatures != null)
                {
                    result.Features.WebFeatures.AddRange(
                        from feature in source.Features.WebFeatures
                        select new Model.Feature
                        {
                            Id = new Guid(feature.ID),
                            Deactivate = feature.Deactivate,
                        });
                }
            }

            // Translate CustomActions, if any
            if (source.CustomActions != null)
            {
                if (result.CustomActions.SiteCustomActions != null && source.CustomActions.SiteCustomActions != null)
                {
                    result.CustomActions.SiteCustomActions.AddRange(
                        from customAction in source.CustomActions.SiteCustomActions
                        select new Model.CustomAction
                        {
                            Description = customAction.Description,
                            Enabled = customAction.Enabled,
                            Group = customAction.Group,
                            ImageUrl = customAction.ImageUrl,
                            Location = customAction.Location,
                            Name = customAction.Name,
                            RightsValue = customAction.RightsSpecified ? customAction.Rights : 0,
                            ScriptBlock = customAction.ScriptBlock,
                            ScriptSrc = customAction.ScriptSrc,
                            Sequence = customAction.SequenceSpecified ? customAction.Sequence : 100,
                            Title = customAction.Title,
                            Url = customAction.Url,
                        });
                }
                if (result.CustomActions.WebCustomActions != null && source.CustomActions.WebCustomActions != null)
                {
                    result.CustomActions.WebCustomActions.AddRange(
                        from customAction in source.CustomActions.WebCustomActions
                        select new Model.CustomAction
                        {
                            Description = customAction.Description,
                            Enabled = customAction.Enabled,
                            Group = customAction.Group,
                            ImageUrl = customAction.ImageUrl,
                            Location = customAction.Location,
                            Name = customAction.Name,
                            RightsValue = customAction.RightsSpecified ? customAction.Rights : 0,
                            ScriptBlock = customAction.ScriptBlock,
                            ScriptSrc = customAction.ScriptSrc,
                            Sequence = customAction.SequenceSpecified ? customAction.Sequence : 100,
                            Title = customAction.Title,
                            Url = customAction.Url,
                        });
                }
            }

            // Translate Files, if any
            if (source.Files != null)
            {
                result.Files.AddRange(
                    from file in source.Files
                    select new Model.File
                    {
                        Overwrite = file.Overwrite,
                        Src = file.Src,
                        Folder = file.Folder,
                    });
            }

            // Translate ComposedLook, if any
            if (source.ComposedLook != null)
            {
                result.ComposedLook.AlternateCSS = source.ComposedLook.AlternateCSS;
                result.ComposedLook.BackgroundFile = source.ComposedLook.BackgroundFile;
                result.ComposedLook.ColorFile = source.ComposedLook.ColorFile;
                result.ComposedLook.FontFile = source.ComposedLook.FontFile;
                result.ComposedLook.MasterPage = source.ComposedLook.MasterPage;
                result.ComposedLook.Name = source.ComposedLook.Name;
                result.ComposedLook.SiteLogo = source.ComposedLook.SiteLogo;
                result.ComposedLook.Version = source.ComposedLook.Version;
            }

            // Translate Providers, if any
            if (source.Providers != null)
            {
                result.ExtensibilityHandlers.AddRange(
                    from provider in source.Providers
                    select new Model.ExtensibilityHandler
                    {
                        Assembly = provider.Assembly,
                        Configuration = provider.Configuration != null ? provider.Configuration.ToProviderConfiguration() : null,
                        Enabled = provider.Enabled,
                        Type = provider.Type,
                    });
            }

            return (result);
        }