Exemplo n.º 1
0
        private void ValidateCreateRequest(CreateContentTypeRequest request)
        {
            if (request == null)
            {
                throw new ValidationException("common.errors.invalidRequest");
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                throw new ValidationException("setting.addOrUpdateContentType.validation.nameIsRequired");
            }

            IContentTypeRepository repo = IoC.Container.Resolve <IContentTypeRepository>();

            if (repo.GetByName(request.Name) != null)
            {
                throw new ValidationException("setting.addOrUpdateContentType.validation.nameAlreadyExisted");
            }

            if (string.IsNullOrWhiteSpace(request.Key))
            {
                throw new ValidationException("setting.addOrUpdateContentType.validation.keyIsRequired");
            }

            if (request.Key.Contains(" "))
            {
                throw new ValidationException("setting.addOrUpdateContentType.validation.keyShouldNotHaveWhiteSpace");
            }

            if (repo.GetByKey(request.Key) != null)
            {
                throw new ValidationException("setting.addOrUpdateContentType.validation.keyAlreadyExisted");
            }
        }
Exemplo n.º 2
0
 public void Create(CreateContentTypeRequest request)
 {
     this.ValidateCreateRequest(request);
     using (IUnitOfWork uow = new UnitOfWork(RepositoryType.MSSQL))
     {
         IContentTypeRepository repo        = IoC.Container.Resolve <IContentTypeRepository>(uow);
         ContentType            contentType = new ContentType(request.Name, request.Key, request.Description);
         this.UpdateParameters(contentType.Id, request.Parameters, uow);
         repo.Add(contentType);
         uow.Commit();
     }
 }
 public void Create(CreateContentTypeRequest request)
 {
     ValidateCreateRequest(request);
     using (IUnitOfWork uow = new UnitOfWork(new AppDbContext(IOMode.Write)))
     {
         IContentTypeRepository repo        = IoC.Container.Resolve <IContentTypeRepository>(uow);
         ContentType            contentType = new ContentType(request.Name, request.Key, request.Description);
         UpdateParameters(contentType.Id, request.Parameters, uow);
         repo.Add(contentType);
         uow.Commit();
     }
 }
Exemplo n.º 4
0
        public ContentType()
        {
            PostMappingHandler = (json) =>
            {
                // Trying to load values with the selectors will result in this not having a StringId sometimes.
                // Prevent a ClientException by checking first
                if (HasValue(nameof(StringId)))
                {
                    // In the case of Add operation, the CSOM api is used and JSON mapping to model is not possible
                    // So here, populate the Rest Id metadata field to enable actions upon
                    // this content type without having to read it again from the server
                    AddMetadata(PnPConstants.MetaDataRestId, StringId);
                    AddMetadata(PnPConstants.MetaDataType, "SP.ContentType");
                    Requested = true;
                }
            };

            // Handler to construct the Add request for this content type
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
            AddApiCallHandler = async(keyValuePairs) =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            {
                // Content type creation by specifying the parent id is not possible using SharePoint REST
                //https://github.com/pnp/pnpjs/issues/457
                //https://github.com/SharePoint/sp-dev-docs/issues/3276
                //https://stackoverflow.com/questions/55529315/how-to-create-site-content-type-with-id-using-rest-api

                // Given this method can apply on both Web.ContentTypes as List.ContentTypes we're getting the entity info which will
                // automatically provide the correct 'parent'
                var entity = EntityManager.GetClassInfo(GetType(), this);

                // Adding new content types on a list is not something we should allow
                if (entity.Target == typeof(List))
                {
                    throw new ClientException(ErrorType.Unsupported,
                                              PnPCoreResources.Exception_Unsupported_AddingContentTypeToList);
                }

                // Fallback to CSOM call
                CreateContentTypeRequest request = new CreateContentTypeRequest(new ContentTypeCreationInfo()
                {
                    Id          = StringId,
                    Description = Description,
                    Group       = Group,
                    Name        = Name
                });

                return(new ApiCall(new List <IRequest <object> >()
                {
                    request
                }));
            };
        }
Exemplo n.º 5
0
        public IResponseData <bool> CreateContentType(CreateContentTypeRequest request)
        {
            IResponseData <bool> response = new ResponseData <bool>();

            try
            {
                IContentTypeService service = IoC.Container.Resolve <IContentTypeService>();
                service.Create(request);
                response.SetData(true);
            }
            catch (ValidationException ex)
            {
                response.SetErrors(ex.Errors);
                response.SetStatus(HttpStatusCode.PreconditionFailed);
            }
            return(response);
        }
        public void CreateContentTypeRequest_Test_GenerateCorrectRequest()
        {
            IteratorIdProvider      idProvider   = new IteratorIdProvider();
            ContentTypeCreationInfo creationInfo = new ContentTypeCreationInfo()
            {
                Id          = "0x10023213123123",
                Description = "Test Description",
                Group       = "Test Group",
                Name        = "Test Name",
            };
            CreateContentTypeRequest request  = new CreateContentTypeRequest(creationInfo);
            List <ActionObjectPath>  requests = request.GetRequest(idProvider);
            var actionRequests = requests.Select(r => r.Action).Where(r => r != null).ToList();
            var identities     = requests.Select(r => r.ObjectPath).Where(id => id != null).ToList();

            BaseAction          objectPath    = actionRequests[0];
            IdentityQueryAction objectIdQuery = actionRequests[1] as IdentityQueryAction;

            ObjectPathMethod createCTAction   = identities[0] as ObjectPathMethod;
            Property         contentTypesProp = identities[1] as Property;
            Property         webProp          = identities[2] as Property;
            StaticProperty   currentSiteProp  = identities[3] as StaticProperty;

            Assert.AreEqual("4", objectPath.ObjectPathId);
            Assert.AreEqual(5, objectPath.Id);

            Assert.AreEqual("4", objectIdQuery.ObjectPathId);
            Assert.AreEqual(6, objectIdQuery.Id);

            Assert.AreEqual("Add", createCTAction.Name);
            Assert.AreEqual(3, createCTAction.ParentId);
            Assert.AreEqual(4, createCTAction.Id);

            Assert.AreEqual("ContentTypes", contentTypesProp.Name);
            Assert.AreEqual(2, contentTypesProp.ParentId);
            Assert.AreEqual(3, contentTypesProp.Id);

            Assert.AreEqual("Web", webProp.Name);
            Assert.AreEqual(1, webProp.ParentId);
            Assert.AreEqual(2, webProp.Id);

            Assert.AreEqual("Current", currentSiteProp.Name);
            Assert.AreEqual("{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}", currentSiteProp.TypeId);
            Assert.AreEqual(1, currentSiteProp.Id);
        }
        public void CreateContentType(CreateContentTypeRequest request)
        {
            IContentTypeService service = IoC.Container.Resolve <IContentTypeService>();

            service.Create(request);
        }