Пример #1
0
        public static void setupComplexListFields(ClientContext ctx)
        {
            Web root = ctx.Site.RootWeb;

            /////////////////////////////////////
            ////////// using xml ///////////////
            ////////////////////////////////////

            string pathToXML = AppDomain.CurrentDomain.BaseDirectory + "complexFields.xml";

            XDocument doc = XDocument.Load(pathToXML);

            List <XElement> LookupElements = doc.Root.Elements("Field").Where(e => e.Attribute("Type").Value == "Lookup").ToList();

            foreach (XElement element in LookupElements)
            {
                string listUrl = element.Attribute("List").Value;
                List   list    = ctx.Web.GetListByUrl(listUrl);
                element.Attribute("List").Value = list.Id.ToString();
            }
            root.CreateFieldsFromXMLString(doc.ToString());

            /////////////////////////////////////
            ////////// Using code ///////////////
            ////////////////////////////////////

            FieldCreationInformation fieldINfo = new FieldCreationInformation(FieldType.Lookup);

            fieldINfo.Id           = "{CC5820F0-60C9-4B9E-9CE9-C739AED70357}".ToGuid();
            fieldINfo.InternalName = "OD1_TryGoodBook2";
            fieldINfo.DisplayName  = "Good book 2";
            fieldINfo.Group        = "Custom Columns";

            FieldLookup lookupField = root.CreateField <FieldLookup>(fieldINfo);

            lookupField.LookupList = root.GetListByUrl("lists/books").Id.ToString();
            lookupField.Update();

            ctx.ExecuteQuery();
        }
Пример #2
0
        private static void ProvisionLists(ClientContext ctx)
        {
            Console.WriteLine("Provisioning lists:");
            Console.WriteLine("Events");
            List eventsList = ctx.Web.CreateList(ListTemplateType.Events, "Events", false, false, "Lists/Events", false);

            eventsList.CreateField(@"<Field Type=""Boolean"" DisplayName=""Registration Allowed"" ID=""{d395011d-07c9-40a5-99c2-cb4d4f209d13}"" Name=""OfficeDevPnPRegistrationAllowed""><Default>1</Default></Field>", false);
            ctx.Load(eventsList);
            ctx.ExecuteQueryRetry();

            Console.WriteLine("Event Registration");
            List  regList = ctx.Web.CreateList(ListTemplateType.GenericList, "Event Registration", false, false, "Lists/Event Registration", false);
            Field field   = regList.CreateField(@"<Field Type=""Lookup"" DisplayName=""Event"" ID=""{39e09239-3da4-455f-9f03-add53034de0a}"" Name=""OfficeDevPnPEventLookup"" />", false);

            ctx.Load(regList);
            ctx.Load(field);
            ctx.ExecuteQueryRetry();

            // configure event lookup field
            FieldLookup eventField = ctx.CastTo <FieldLookup>(field);

            eventField.LookupList                 = eventsList.Id.ToString();
            eventField.LookupField                = "Title";
            eventField.Indexed                    = true;
            eventField.IsRelationship             = true;
            eventField.RelationshipDeleteBehavior = RelationshipDeleteBehaviorType.Cascade;
            eventField.Update();
            ctx.ExecuteQueryRetry();
            // configure author field
            Field authorField = regList.Fields.GetFieldByName <Field>("Author");

            authorField.Indexed = true;
            authorField.Update();
            ctx.ExecuteQueryRetry();

            Console.WriteLine("");
        }
Пример #3
0
        public static void ProvisionLists(ClientContext ctx)
        {
            //Create country list
            Console.WriteLine("Provisioning lists:");
            List countryList = null;

            if (!ctx.Web.ListExists("EmpCountry"))
            {
                Console.WriteLine("Country list...");
                countryList = ctx.Web.CreateList(ListTemplateType.GenericList, "EmpCountry", false, false, "Lists/EmpCountry", false);

                //Provision country list items
                ListItemCreationInformation newCountryCreationInfomation;
                newCountryCreationInfomation = new ListItemCreationInformation();
                ListItem newCountry = countryList.AddItem(newCountryCreationInfomation);
                newCountry["Title"] = "Belgium";
                newCountry.Update();
                newCountry          = countryList.AddItem(newCountryCreationInfomation);
                newCountry["Title"] = "United States of America";
                newCountry.Update();
                newCountry          = countryList.AddItem(newCountryCreationInfomation);
                newCountry["Title"] = "India";
                newCountry.Update();
                ctx.Load(countryList);
                ctx.ExecuteQueryRetry();
            }
            else
            {
                countryList = ctx.Web.GetListByUrl("Lists/EmpCountry");
                Console.WriteLine("Country list was already available");
            }

            List stateList = null;

            if (!ctx.Web.ListExists("EmpState"))
            {
                Console.WriteLine("State list...");
                stateList = ctx.Web.CreateList(ListTemplateType.GenericList, "EmpState", false, false, "Lists/EmpState", false);
                Field countryLookup = stateList.CreateField(@"<Field Type=""Lookup"" DisplayName=""Country"" ID=""{BDEF775C-AB4B-4E86-9FB8-0A2DE40FE832}"" Name=""Country""></Field>", false);
                ctx.Load(stateList);
                ctx.Load(countryLookup);
                ctx.Load(stateList.DefaultView, p => p.ViewFields);
                ctx.ExecuteQueryRetry();

                // Add field to default view
                stateList.DefaultView.ViewFields.Add("Country");
                stateList.DefaultView.Update();
                ctx.ExecuteQueryRetry();

                // configure country lookup field
                FieldLookup countryField = ctx.CastTo <FieldLookup>(countryLookup);
                countryField.LookupList                 = countryList.Id.ToString();
                countryField.LookupField                = "Title";
                countryField.Indexed                    = true;
                countryField.IsRelationship             = true;
                countryField.RelationshipDeleteBehavior = RelationshipDeleteBehaviorType.Restrict;
                countryField.Update();
                ctx.ExecuteQueryRetry();

                //Provision state list items
                ListItemCreationInformation newStateCreationInfomation;
                newStateCreationInfomation = new ListItemCreationInformation();
                ListItem newState = stateList.AddItem(newStateCreationInfomation);
                newState["Title"]   = "Washington";
                newState["Country"] = "2;#United States of America";
                newState.Update();
                newState            = stateList.AddItem(newStateCreationInfomation);
                newState["Title"]   = "Limburg";
                newState["Country"] = "1;#Belgium";
                newState.Update();
                newState            = stateList.AddItem(newStateCreationInfomation);
                newState["Title"]   = "Tennessee";
                newState["Country"] = "2;#United States of America";
                newState.Update();
                newState            = stateList.AddItem(newStateCreationInfomation);
                newState["Title"]   = "Karnataka";
                newState["Country"] = "3;#India";
                newState.Update();

                ctx.ExecuteQueryRetry();
            }
            else
            {
                countryList = ctx.Web.GetListByUrl("Lists/EmpState");
                Console.WriteLine("State list was already available");
            }

            List cityList = null;

            if (!ctx.Web.ListExists("EmpCity"))
            {
                Console.WriteLine("City list...");
                cityList = ctx.Web.CreateList(ListTemplateType.GenericList, "EmpCity", false, false, "Lists/EmpCity", false);
                Field stateLookup = cityList.CreateField(@"<Field Type=""Lookup"" DisplayName=""State"" ID=""{F55BED78-CAF9-4EDF-92B9-C46BDC032DD5}"" Name=""State""></Field>", false);
                ctx.Load(cityList);
                ctx.Load(stateLookup);
                ctx.Load(cityList.DefaultView, p => p.ViewFields);
                ctx.ExecuteQueryRetry();

                // Add field to default view
                cityList.DefaultView.ViewFields.Add("State");
                cityList.DefaultView.Update();
                ctx.ExecuteQueryRetry();

                // configure state lookup field
                FieldLookup stateField = ctx.CastTo <FieldLookup>(stateLookup);
                stateField.LookupList                 = stateList.Id.ToString();
                stateField.LookupField                = "Title";
                stateField.Indexed                    = true;
                stateField.IsRelationship             = true;
                stateField.RelationshipDeleteBehavior = RelationshipDeleteBehaviorType.Restrict;
                stateField.Update();
                ctx.ExecuteQueryRetry();

                //Provision city list items
                ListItemCreationInformation newCityCreationInfomation;
                newCityCreationInfomation = new ListItemCreationInformation();
                ListItem newCity = cityList.AddItem(newCityCreationInfomation);
                newCity["Title"] = "Bree";
                newCity["State"] = "2;#Limburg";
                newCity.Update();
                newCity          = cityList.AddItem(newCityCreationInfomation);
                newCity["Title"] = "Redmond";
                newCity["State"] = "1;#Washington";
                newCity.Update();
                newCity          = cityList.AddItem(newCityCreationInfomation);
                newCity["Title"] = "Franklin";
                newCity["State"] = "3;#Tennessee";
                newCity.Update();
                newCity          = cityList.AddItem(newCityCreationInfomation);
                newCity["Title"] = "Bangalore";
                newCity["State"] = "4;#Karnataka";
                newCity.Update();

                ctx.ExecuteQueryRetry();
            }
            else
            {
                cityList = ctx.Web.GetListByUrl("Lists/EmpCity");
                Console.WriteLine("City list was already available");
            }

            List designationList = null;

            if (!ctx.Web.ListExists("EmpDesignation"))
            {
                Console.WriteLine("Designation list...");
                designationList = ctx.Web.CreateList(ListTemplateType.GenericList, "EmpDesignation", false, false, "Lists/EmpDesignation", false);
                ctx.Load(designationList);
                ctx.ExecuteQueryRetry();

                //Provision designation list items
                ListItemCreationInformation newDesignationCreationInfomation;
                newDesignationCreationInfomation = new ListItemCreationInformation();
                ListItem newDesignation = designationList.AddItem(newDesignationCreationInfomation);
                newDesignation["Title"] = "Service Engineer";
                newDesignation.Update();
                newDesignation          = designationList.AddItem(newDesignationCreationInfomation);
                newDesignation["Title"] = "Service Engineer II";
                newDesignation.Update();
                newDesignation          = designationList.AddItem(newDesignationCreationInfomation);
                newDesignation["Title"] = "Senior Service Engineer";
                newDesignation.Update();
                newDesignation          = designationList.AddItem(newDesignationCreationInfomation);
                newDesignation["Title"] = "Principal Service Engineer";
                newDesignation.Update();
                newDesignation          = designationList.AddItem(newDesignationCreationInfomation);
                newDesignation["Title"] = "Program Manager";
                newDesignation.Update();
                newDesignation          = designationList.AddItem(newDesignationCreationInfomation);
                newDesignation["Title"] = "Senior Program Manager";
                newDesignation.Update();

                ctx.ExecuteQueryRetry();
            }
            else
            {
                designationList = ctx.Web.GetListByUrl("Lists/EmpDesignation");
                Console.WriteLine("Designation list was already available");
            }

            List empAttachmentsList = null;

            if (!ctx.Web.ListExists("EmpAttachments"))
            {
                Console.WriteLine("EmpAttachments list...");
                empAttachmentsList = ctx.Web.CreateList(ListTemplateType.DocumentLibrary, "EmpAttachments", false, false, "Lists/EmpAttachments", false);
                ctx.Load(empAttachmentsList);
                ctx.ExecuteQueryRetry();

                empAttachmentsList.CreateField(@"<Field Type=""Text"" DisplayName=""AttachmentID"" ID=""{E6FAC176-F466-4174-9785-7FB9611CBC00}"" Name=""AttachmentID""></Field>", false);
                empAttachmentsList.Update();
                ctx.Load(empAttachmentsList.DefaultView, p => p.ViewFields);
                ctx.ExecuteQueryRetry();

                // Add fields to view
                empAttachmentsList.DefaultView.ViewFields.Add("AttachmentID");
                empAttachmentsList.DefaultView.Update();
                ctx.ExecuteQueryRetry();
            }
            else
            {
                empAttachmentsList = ctx.Web.GetListByUrl("Lists/EmpAttachments");
                Console.WriteLine("EmpAttachments list was already available");
            }

            List employeeList = null;

            if (!ctx.Web.ListExists("Employees"))
            {
                Console.WriteLine("Employee list...");
                employeeList = ctx.Web.CreateList(ListTemplateType.GenericList, "Employees", false, false, "Lists/Employees", false);
                ctx.Load(employeeList);
                ctx.ExecuteQueryRetry();

                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""Number"" ID=""{0FFF75B6-0E57-46BE-84D7-E5225A55E914}"" Name=""EmpNumber""></Field>", false);
                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""UserID"" ID=""{D3199531-C091-4359-8CDE-86FB3924F65E}"" Name=""UserID""></Field>", false);
                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""Manager"" ID=""{B7E2F4D9-AEA2-40DB-A354-94EEDAFEF35E}"" Name=""EmpManager""></Field>", false);
                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""Designation"" ID=""{AB230804-C137-4ED8-A6D6-722037BDDA3D}"" Name=""Designation""></Field>", false);
                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""Location"" ID=""{2EE32832-5EF0-41D0-8CD3-3DE7B9616C21}"" Name=""Location""></Field>", false);
                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""Skills"" ID=""{89C02660-822D-4F41-881D-1D533C56017E}"" Name=""Skills""></Field>", false);
                employeeList.CreateField(@"<Field Type=""Text"" DisplayName=""AttachmentID"" ID=""{53A31281-1C25-4D00-B785-40C71D37AE7B}"" Name=""AttachmentID""></Field>", false);
                employeeList.Update();
                ctx.Load(employeeList.DefaultView, p => p.ViewFields);
                ctx.ExecuteQueryRetry();

                // Add fields to view
                employeeList.DefaultView.ViewFields.Add("EmpNumber");
                employeeList.DefaultView.ViewFields.Add("UserID");
                employeeList.DefaultView.ViewFields.Add("EmpManager");
                employeeList.DefaultView.ViewFields.Add("Designation");
                employeeList.DefaultView.ViewFields.Add("Location");
                employeeList.DefaultView.ViewFields.Add("Skills");
                employeeList.DefaultView.ViewFields.Add("AttachmentID");
                employeeList.DefaultView.Update();
                ctx.ExecuteQueryRetry();
            }
            else
            {
                employeeList = ctx.Web.GetListByUrl("Lists/Employees");
                Console.WriteLine("Employee list was already available");
            }
        }
Пример #4
0
        static void Main()
        {
            string        url           = "http://intranet.wingtip.com";
            ClientContext clientContext = new ClientContext(url);

            Web site = clientContext.Web;

            clientContext.ExecuteQuery();

            ExceptionHandlingScope scope1 = new ExceptionHandlingScope(clientContext);

            using (scope1.StartScope()) {
                using (scope1.StartTry()) {
                    site.Lists.GetByTitle("Teams").DeleteObject();
                }
                using (scope1.StartCatch()) { }
            }

            ExceptionHandlingScope scope2 = new ExceptionHandlingScope(clientContext);

            using (scope2.StartScope()) {
                using (scope2.StartTry()) {
                    site.Lists.GetByTitle("Players").DeleteObject();
                }
                using (scope2.StartCatch()) { }
            }



            ListCreationInformation ciTeams = new ListCreationInformation();

            ciTeams.Title             = "Teams";
            ciTeams.Url               = "Lists/Teams";
            ciTeams.QuickLaunchOption = QuickLaunchOptions.On;
            ciTeams.TemplateType      = (int)ListTemplateType.GenericList;

            List Teams = site.Lists.Add(ciTeams);

            Teams.EnableAttachments = false;
            Teams.Update();

            ListItem team1 = Teams.AddItem(new ListItemCreationInformation());

            team1["Title"] = "Boston Celtics";
            team1.Update();

            ListItem team2 = Teams.AddItem(new ListItemCreationInformation());

            team2["Title"] = "LA Lakers";
            team2.Update();

            clientContext.Load(Teams);
            clientContext.ExecuteQuery();


            ListCreationInformation ciPlayers = new ListCreationInformation();

            ciPlayers.Title             = "Players";
            ciPlayers.Url               = "Lists/Players";
            ciPlayers.QuickLaunchOption = QuickLaunchOptions.On;
            ciPlayers.TemplateType      = (int)ListTemplateType.GenericList;

            List Players = site.Lists.Add(ciPlayers);

            Players.EnableAttachments = false;
            Players.Update();

            string fieldXML = @"<Field Name='Team' " +
                              "DisplayName='Team' " +
                              "Type='Lookup' > " +
                              "</Field>";

            FieldLookup lookup = clientContext.CastTo <FieldLookup>(Players.Fields.AddFieldAsXml(fieldXML, true, AddFieldOptions.DefaultValue));

            lookup.LookupField = "Title";
            lookup.LookupList  = Teams.Id.ToString();
            lookup.Update();

            Console.WriteLine("ID: " + Teams.Id.ToString());

            clientContext.ExecuteQuery();
        }
        public static void CreateOrderDetailsList()
        {
            Console.WriteLine("Creating order details list...");

            if (OrdersListExists())
            {
                ListCreationInformation listInformationOrderDetails = new ListCreationInformation();
                listInformationOrderDetails.Title             = "OrderDetails";
                listInformationOrderDetails.Url               = "Lists/OrderDetails";
                listInformationOrderDetails.QuickLaunchOption = QuickLaunchOptions.On;
                listInformationOrderDetails.TemplateType      = (int)ListTemplateType.GenericList;
                listOrderDetails = site.Lists.Add(listInformationOrderDetails);
                listOrderDetails.OnQuickLaunch     = true;
                listOrderDetails.EnableAttachments = false;
                listOrderDetails.Update();
                clientContext.ExecuteQuery();

                listOrderDetails.DefaultView.ViewFields.RemoveAll();
                listOrderDetails.DefaultView.ViewFields.Add("ID");
                listOrderDetails.DefaultView.Update();
                clientContext.ExecuteQuery();

                var fldTitle = listOrderDetails.Fields.GetByInternalNameOrTitle("Title");
                fldTitle.Required = false;
                fldTitle.Update();
                clientContext.ExecuteQuery();

                string      fldOrderLookupXml = @"<Field Name='OrderId' DisplayName='Order' Type='Lookup' ></Field>";
                FieldLookup fldOrderLookup    =
                    clientContext.CastTo <FieldLookup>(listOrderDetails.Fields.AddFieldAsXml(fldOrderLookupXml,
                                                                                             true,
                                                                                             AddFieldOptions.AddFieldInternalNameHint));

                // add cusotmer lookup field
                fldOrderLookup.LookupField = "ID";
                fldOrderLookup.LookupList  = listOrders.Id.ToString();
                fldOrderLookup.Indexed     = true;
                fldOrderLookup.RelationshipDeleteBehavior = RelationshipDeleteBehaviorType.Cascade;
                fldOrderLookup.Update();

                // add quantity field
                string      fldQuantityXml = @"<Field Name='Quantity' DisplayName='Quantity' Type='Number' ></Field>";
                FieldNumber fldQuantity    =
                    clientContext.CastTo <FieldNumber>(listOrderDetails.Fields.AddFieldAsXml(fldQuantityXml,
                                                                                             true,
                                                                                             AddFieldOptions.DefaultValue));
                fldQuantity.Update();

                // add product field
                string    fldProductXml = @"<Field Name='Product' DisplayName='Product' Type='Text' ></Field>";
                FieldText fldProduct    =
                    clientContext.CastTo <FieldText>(listOrderDetails.Fields.AddFieldAsXml(fldProductXml,
                                                                                           true,
                                                                                           AddFieldOptions.DefaultValue));
                fldProduct.Update();

                string        fldSalesAmountXml = @"<Field Name='SalesAmount' DisplayName='SalesAmount' Type='Currency' ></Field>";
                FieldCurrency fldSalesAmount    =
                    clientContext.CastTo <FieldCurrency>(listOrderDetails.Fields.AddFieldAsXml(fldSalesAmountXml,
                                                                                               true,
                                                                                               AddFieldOptions.DefaultValue));
                fldSalesAmount.Update();

                clientContext.ExecuteQuery();

                //listOrderDetails.DefaultView.ViewFields.Remove("Title");
                listOrderDetails.DefaultView.Update();
                clientContext.ExecuteQuery();
            }
            else
            {
                Console.WriteLine("Cannot create OrderDetails list because Orders list does not exist.");
            }
        }
        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.");
            }
        }