static void CreateExpenseSiteColumns() { fldExpenseCategory = clientContext.CastTo <FieldChoice>(CreateSiteColumn("ExpenseCategory", "Expense Category", "Choice")); string[] choicesExpenseCategory = ExpenseCategory.GetAll(); fldExpenseCategory.Choices = choicesExpenseCategory; fldExpenseCategory.Update(); clientContext.ExecuteQuery(); fldExpenseDate = clientContext.CastTo <FieldDateTime>(CreateSiteColumn("ExpenseDate", "Expense Date", "DateTime"));; fldExpenseDate.DisplayFormat = DateTimeFieldFormatType.DateOnly; fldExpenseDate.Update(); fldExpenseAmount = clientContext.CastTo <FieldCurrency>(CreateSiteColumn("ExpenseAmount", "Expense Amount", "Currency")); fldExpenseAmount.MinimumValue = 0; fldExpenseBudgetYear = clientContext.CastTo <FieldText>(CreateSiteColumn("ExpenseBudgetYear", "Budget Year", "Text")); fldExpenseBudgetQuarter = clientContext.CastTo <FieldText>(CreateSiteColumn("ExpenseBudgetQuarter", "Budget Quarter", "Text")); fldExpenseBudgetQuarter.Update(); fldExpenseBudgetAmount = clientContext.CastTo <FieldCurrency>(CreateSiteColumn("ExpenseBudgetAmount", "Budget Amount", "Currency")); clientContext.ExecuteQuery(); }
private static void CreateFields(ClientContext ctx, ContentType customerCT) { #region Customer logo FieldCreationInformation customerLogo = new FieldCreationInformation(FieldType.URL); customerLogo.DisplayName = "Logo"; customerLogo.InternalName = "DispLogo"; customerLogo.Group = "ODA1"; customerLogo.Id = Constants.GUID.CustomerCT.CUSTOMER_LOGO.ToGuid(); FieldUrl AddedCLOGO = ctx.Web.CreateField <FieldUrl>(customerLogo, false); //AddedCLOGO.DisplayFormat = UrlFieldFormatType.Image; //AddedCLOGO.Update(); ctx.ExecuteQuery(); ctx.Web.AddFieldToContentType(customerCT, AddedCLOGO); #endregion #region Address FieldCreationInformation address = new FieldCreationInformation(FieldType.Text); address.DisplayName = "Address"; address.InternalName = "Address"; address.Group = "ODA1"; address.Id = Constants.GUID.CustomerCT.ADDRESS.ToGuid(); ctx.Web.AddFieldToContentType(customerCT, ctx.Web.CreateField(address)); #endregion #region Main Contact Person FieldCreationInformation contactPerson = new FieldCreationInformation(FieldType.Text); contactPerson.DisplayName = "Contact Person"; contactPerson.InternalName = "contactPerson"; contactPerson.Group = "ODA1"; contactPerson.Id = Constants.GUID.CustomerCT.MAIN_CONTACT_PERSON.ToGuid(); ctx.Web.AddFieldToContentType(customerCT, ctx.Web.CreateField(contactPerson)); #endregion #region Office Phone FieldCreationInformation phoneOffice = new FieldCreationInformation(FieldType.Text); phoneOffice.DisplayName = "Office Phone"; phoneOffice.InternalName = "phoneOffice"; phoneOffice.Group = "ODA1"; phoneOffice.Id = Constants.GUID.CustomerCT.PHONE_OFFICE.ToGuid(); ctx.Web.AddFieldToContentType(customerCT, ctx.Web.CreateField(phoneOffice)); #endregion #region Mobile Phone FieldCreationInformation phoneMobile = new FieldCreationInformation(FieldType.Text); phoneMobile.DisplayName = "Mobile"; phoneMobile.InternalName = "phoneMobile"; phoneMobile.Group = "ODA1"; phoneMobile.Id = Constants.GUID.CustomerCT.PHONE_MOBILE.ToGuid(); ctx.Web.AddFieldToContentType(customerCT, ctx.Web.CreateField(phoneMobile)); #endregion #region Email FieldCreationInformation email = new FieldCreationInformation(FieldType.Text); email.DisplayName = "E-Mail"; email.InternalName = "Email"; email.Group = "ODA1"; email.Id = Constants.GUID.CustomerCT.EMAIL.ToGuid(); ctx.Web.AddFieldToContentType(customerCT, ctx.Web.CreateField(email)); #endregion #region Last Contacted (Date) FieldCreationInformation lastContacted = new FieldCreationInformation(FieldType.DateTime); lastContacted.DisplayName = "Last Contacted"; lastContacted.InternalName = "LastContacted"; lastContacted.Group = "ODA1"; lastContacted.Id = Constants.GUID.CustomerCT.LAST_CONTACTED.ToGuid(); ctx.Web.AddFieldToContentType(customerCT, ctx.Web.CreateField(lastContacted)); #endregion #region Last Order Made(Date, Read Only) FieldCreationInformation lastOrderMade = new FieldCreationInformation(FieldType.DateTime); lastOrderMade.DisplayName = "Last order made"; lastOrderMade.InternalName = "LastOrderMade"; lastOrderMade.Group = "ODA1"; lastOrderMade.Id = Constants.GUID.CustomerCT.LAST_ORDER_MADE.ToGuid(); FieldDateTime addedLastOderMade = ctx.Web.CreateField <FieldDateTime>(lastOrderMade, false); addedLastOderMade.ReadOnlyField = true; addedLastOderMade.Update(); ctx.ExecuteQuery(); ctx.Web.AddFieldToContentType(customerCT, addedLastOderMade); #endregion }
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties) { SPRemoteEventResult result = new SPRemoteEventResult(); //get the host web url Uri hostUrl = properties.AppEventProperties.HostWebFullUrl; //get the operation context so we can figure out the host URL for this service, from //which we can get the Authority for a client context System.ServiceModel.OperationContext oc = System.ServiceModel.OperationContext.Current; Uri localUrl = null; //UPDATE: THIS CODE WORKED FINE FOR WHEN YOU USE THE WEB APP CREATED BY VS.NET //FOR THE SHAREPOINT APP, BUT IT BREAKS WHEN YOU DEPLOY TO A REAL IIS SERVER //BECAUSE YOU END UP GETTING TWO BASEADDRESSES, BUT NOT WITH THE RIGHT SCHEME. //FOR EXAMPLE, THE FIRST ONE IS THE ADDRESS OF THIS HOST BUT WITH THE HTTP SCHEME. //THE SECOND ONE IS HTTPS, BUT THE HOST NAME IS THE FQDN OF THE SERVER. SINCE //SHAREPOINT DOESN'T RECOGNIZE THAT AS THE ENDPOINT IT TRUSTS FOR THIS CODE, IT BLOWS //UP WHEN RUNNING THE CODE BELOW AND THE WHOLE THING FAILS #region Code That Breaks In IIS ////now enumerate through the Host base addresses and look for the SSL connection //foreach (Uri u in oc.Host.BaseAddresses) //{ // if (u.Scheme.ToLower() == "https") // { // localUrl = u; // break; // } //} #endregion //assume first base address is ours, which it has been so far if (oc.Host.BaseAddresses.Count > 0) { localUrl = oc.Host.BaseAddresses[0]; } //make sure we found our local URL if (localUrl != null) { //using (ClientContext ctx = TokenHelper.CreateAppEventClientContext(properties, false)) using (ClientContext ctx = TokenHelper.GetClientContextWithContextToken(hostUrl.ToString(), properties.ContextToken, localUrl.Authority)) { if (ctx != null) { //try to retrieve the list first to see if it exists List l = ctx.Web.Lists.GetByTitle(LIST_NAME); ctx.Load(l); //have to put in a try block because of course it throw an exception if //list doesn't exist try { ctx.ExecuteQuery(); } catch (Exception noListEx) { //look to see if the exception is that the list doesn't exist if (noListEx.Message.ToLower().Contains("does not exist")) { //code here to create list Web web = ctx.Web; ListCreationInformation ci = new ListCreationInformation(); ci.Title = LIST_NAME; ci.TemplateType = (int)ListTemplateType.GenericList; ci.QuickLaunchOption = QuickLaunchOptions.Off; l = web.Lists.Add(ci); l.Description = "List for tracking events with the Event Planner Social App"; Field fldEventName = l.Fields.AddFieldAsXml("<Field DisplayName='EventName' Type='Text' />", true, AddFieldOptions.DefaultValue); Field fldSiteUrl = l.Fields.AddFieldAsXml("<Field DisplayName='SiteUrl' Type='Text' />", true, AddFieldOptions.DefaultValue); Field fldTwitterTags = l.Fields.AddFieldAsXml("<Field DisplayName='TwitterTags' Type='Text' />", true, AddFieldOptions.DefaultValue); Field fldEventDate = l.Fields.AddFieldAsXml("<Field DisplayName='EventDate' Type='DateTime' />", true, AddFieldOptions.DefaultValue); FieldDateTime dtEventDate = ctx.CastTo <FieldDateTime>(fldEventDate); dtEventDate.DisplayFormat = DateTimeFieldFormatType.DateOnly; dtEventDate.Update(); Field fldGraphID = l.Fields.AddFieldAsXml("<Field DisplayName='ObjectGraphID' Type='Text' />", true, AddFieldOptions.DefaultValue); FieldText txtGraphID = ctx.CastTo <FieldText>(fldGraphID); txtGraphID.Indexed = true; txtGraphID.Update(); l.Hidden = true; l.Update(); try { //this creates the list ctx.ExecuteQuery(); //all of the rest of this is to remove the list from the "Recent" list that appears //in sites by default, which is really a set of navigation links //get the site and root web, where the navigation lives Site s = ctx.Site; Web rw = s.RootWeb; //get the QuickLaunch navigation, which is where the Recent nav lives ctx.Load(rw, x => x.Navigation, x => x.Navigation.QuickLaunch); ctx.ExecuteQuery(); //now extract the Recent navigation node from the collection var vNode = from NavigationNode nn in rw.Navigation.QuickLaunch where nn.Title == "Recent" select nn; NavigationNode nNode = vNode.First <NavigationNode>(); //now we need to get the child nodes of Recent, that's where our list should be found ctx.Load(nNode.Children); ctx.ExecuteQuery(); var vcNode = from NavigationNode cn in nNode.Children where cn.Title == LIST_NAME select cn; //now that we have the node representing our list, delete it NavigationNode cNode = vcNode.First <NavigationNode>(); cNode.DeleteObject(); ctx.ExecuteQuery(); } catch (Exception newListFailEx) { Debug.WriteLine("Creation of new list failed: " + newListFailEx.Message); } } } //okay, so if we're here then the list should exist, and we should be good to go at this point } } } #region OOB Template Code //using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false)) //{ // if (clientContext != null) // { // clientContext.Load(clientContext.Web); // clientContext.ExecuteQuery(); // } //} #endregion return(result); }
public static void CreateBookCT(ClientContext ctx) { string bookCT = "0x01000E870749A9444905BB8A362E475B0798"; Web web = ctx.Site.RootWeb; //web.GetListByTitle("Books2").DeleteObject(); //ctx.ExecuteQuery(); //web.DeleteContentTypeById(bookCT); if (!web.ContentTypeExistsById(bookCT)) { web.CreateContentType("David Books", bookCT, "Davids ContentType"); } string bookTypeFieldId = "{DBB24705-0DEA-4C4F-8C2A-95CB6F0DE25E}"; if (!web.FieldExistsById(new Guid(bookTypeFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Choice); info.Id = bookTypeFieldId.ToGuid(); info.InternalName = "DAV_BookType"; info.DisplayName = "Book Type"; info.Group = "Tims Columns"; FieldChoice field = web.CreateField <FieldChoice>(info); field.Choices = new string[] { "Romance", "Drama", "Horror", "Thriller" }; field.Update(); ctx.ExecuteQuery(); } string authorFieldId = "{D6996667-0BEA-4C9F-9904-DEB21CC5AA84}"; if (!web.FieldExistsById(new Guid(authorFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Text); info.Id = authorFieldId.ToGuid(); info.InternalName = "DAV_Author"; info.DisplayName = "Author"; info.Group = "Tims Columns"; Field field = web.CreateField(info); } string releaseDateFieldId = "{84716863-06CA-4D31-BAA0-7D099FC501E7}"; if (!web.FieldExistsById(new Guid(releaseDateFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.DateTime); info.Id = releaseDateFieldId.ToGuid(); info.InternalName = "DAV_Realesedate"; info.DisplayName = "ReleaseDate"; info.Group = "Tims Columns"; FieldDateTime field = web.CreateField <FieldDateTime>(info); field.DisplayFormat = DateTimeFieldFormatType.DateOnly; field.Update(); ctx.ExecuteQuery(); } string descriptionDateFieldId = "{4BD3F599-4D5C-412D-8431-6ECD36AEB015}"; // web.RemoveFieldById(descriptionDateFieldId); if (!web.FieldExistsById(new Guid(descriptionDateFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Note); info.Id = descriptionDateFieldId.ToGuid(); info.InternalName = "DAV_description"; info.DisplayName = "Description"; info.Group = "Tims Columns"; info.Required = true; FieldMultiLineText field = web.CreateField <FieldMultiLineText>(info); field.RichText = true; field.NumberOfLines = 10; field.AllowHyperlink = true; field.Update(); ctx.ExecuteQuery(); } web.AddFieldToContentTypeById(bookCT, bookTypeFieldId); web.AddFieldToContentTypeById(bookCT, authorFieldId); web.AddFieldToContentTypeById(bookCT, releaseDateFieldId); web.AddFieldToContentTypeById(bookCT, descriptionDateFieldId, true); if (!web.ListExists("Books2")) { List list = web.CreateList(ListTemplateType.GenericList, "Books2", false, urlPath: "lists/books2", enableContentTypes: true); list.AddContentTypeToListById(bookCT, true); View listView = list.DefaultView; listView.ViewFields.Add("DAV_BookType"); listView.ViewFields.Add("DAV_Author"); listView.ViewFields.Add("DAV_Realesedate"); listView.ViewFields.Add("DAV_description"); listView.Update(); ctx.ExecuteQueryRetry(); } List bookList = web.GetListByTitle("Books2"); ListItem item = bookList.AddItem(new ListItemCreationInformation()); item["Title"] = "MistBorn"; item["DAV_BookType"] = "Fantasy"; item["DAV_Author"] = "Brandon Sanderson"; item["DAV_Realesedate"] = DateTime.Parse("2001-02-12"); item["DAV_description"] = "This is a decription \n\n is this a new line?"; item.Update(); ctx.ExecuteQuery(); //ListItemCollection items = bookList.GetItems(CamlQuery.CreateAllItemsQuery()); //ctx.Load(items); //ctx.ExecuteQuery(); }
public static void CreateOrdersList() { Console.WriteLine("Creating orders list..."); if (CustomersListExists()) { ListCreationInformation listInformationOrders = new ListCreationInformation(); listInformationOrders.Title = "Orders"; listInformationOrders.Url = "Lists/Orders"; listInformationOrders.QuickLaunchOption = QuickLaunchOptions.On; listInformationOrders.TemplateType = (int)ListTemplateType.GenericList; listOrders = site.Lists.Add(listInformationOrders); listOrders.OnQuickLaunch = true; listOrders.EnableAttachments = false; listOrders.Update(); clientContext.ExecuteQuery(); clientContext.Load(listOrders.DefaultView.ViewFields); clientContext.ExecuteQuery(); listOrders.DefaultView.ViewFields.RemoveAll(); listOrders.DefaultView.ViewFields.Add("ID"); listOrders.DefaultView.ViewFields.Add("Title"); listOrders.DefaultView.Update(); clientContext.ExecuteQuery(); string fldCustomerLookupXml = @"<Field Name='Customer' DisplayName='Customer' Type='Lookup' ></Field>"; FieldLookup fldCustomerLookup = clientContext.CastTo <FieldLookup>(listOrders.Fields.AddFieldAsXml(fldCustomerLookupXml, true, AddFieldOptions.DefaultValue)); // add cusotmer lookup field fldCustomerLookup.LookupField = "Title"; fldCustomerLookup.LookupList = listCustomers.Id.ToString(); fldCustomerLookup.Indexed = true; fldCustomerLookup.RelationshipDeleteBehavior = RelationshipDeleteBehaviorType.Cascade; fldCustomerLookup.Update(); // add order date field string fldOrderDateXml = @"<Field Name='OrderDate' DisplayName='OrderDate' Type='DateTime' ></Field>"; FieldDateTime fldOrderDate = clientContext.CastTo <FieldDateTime>(listOrders.Fields.AddFieldAsXml(fldOrderDateXml, true, AddFieldOptions.DefaultValue)); fldOrderDate.DisplayFormat = DateTimeFieldFormatType.DateOnly; fldOrderDate.Update(); // add order date field string fldOrderAmountXml = @"<Field Name='OrderAmount' DisplayName='OrderAmount' Type='Currency' ></Field>"; FieldCurrency fldOrderAmount = clientContext.CastTo <FieldCurrency>(listOrders.Fields.AddFieldAsXml(fldOrderAmountXml, true, AddFieldOptions.DefaultValue)); fldOrderAmount.Update(); clientContext.ExecuteQuery(); clientContext.Load(listOrders); clientContext.ExecuteQuery(); } else { Console.WriteLine("Cannot create Orders list because Customer list does not exist."); } }