public ActionResult AddWelcomeFieldAction([Bind(Include = "SelectedFieldId")] AddWelcomeFieldViewModel model)
        {
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                clientContext.Load(clientContext.Web, w => w.ContentTypes);
                clientContext.ExecuteQuery();

                ContentType docSetContentType = clientContext.Web.ContentTypes.FirstOrDefault(ct => ct.Name == CONTENTTYPENAME);
                Field       selectedField     = clientContext.Web.Fields.GetById(model.SelectedFieldId);
                clientContext.Load(selectedField);
                clientContext.ExecuteQuery();

                if (docSetContentType != null && selectedField.ServerObjectIsNull == false)
                {
                    DocumentSetTemplate docSetTemplate = DocumentSetTemplate.GetDocumentSetTemplate(clientContext, docSetContentType);
                    clientContext.Load(docSetTemplate, d => d.WelcomePageFields);
                    clientContext.ExecuteQuery();


                    if (!docSetTemplate.WelcomePageFields.Contains(selectedField))
                    {
                        docSetTemplate.WelcomePageFields.Add(selectedField);
                        docSetTemplate.Update(true);
                        clientContext.ExecuteQuery();
                    }
                }
            }
            return(RedirectToAction("Index", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
        }
        public ActionResult AddWelcomeField()
        {
            AddWelcomeFieldViewModel model = new AddWelcomeFieldViewModel();

            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    Web web = clientContext.Web;
                    clientContext.Load(web, w => w.ContentTypes, w => w.Fields);
                    clientContext.ExecuteQuery();

                    ContentType docSetContentType = web.ContentTypes.FirstOrDefault(ct => ct.Name == CONTENTTYPENAME);
                    if (docSetContentType != null)
                    {
                        clientContext.Load(docSetContentType, ct => ct.Fields);
                        clientContext.ExecuteQuery();

                        List <FieldModel> fieldModels = new List <FieldModel>();
                        foreach (Field field in docSetContentType.Fields)
                        {
                            fieldModels.Add(new FieldModel()
                            {
                                Name = field.Title, Id = field.Id, Type = field.TypeAsString
                            });
                        }
                        ViewBag.SelectedFieldId = new SelectList(fieldModels, "Id", "Name", model.SelectedFieldId);
                    }
                }
            }
            return(View("AddWelcomeField", model));
        }