public void InsertData(ClientContext clientContext, DKBSDbContext dbContext) { try { Console.WriteLine(" Successfully Connected"); SP.List oList = clientContext.Web.Lists.GetByTitle("Partnere"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>"; ListItemCollection collListItem = oList.GetItems(camlQuery); clientContext.Load(collListItem); clientContext.ExecuteQuery(); foreach (ListItem oListItem in collListItem) { var hyperLink = ((SP.FieldUrlValue)(oListItem["CISite"])); if (hyperLink != null) { Console.WriteLine("ID: {0} \nTitle: {1} \nSite: {2} \nSiteUrl: {3} ", oListItem.Id, oListItem["Title"], oListItem["CISite"], oListItem["CISiteShortUrl"]); var hLink = ((SP.FieldUrlValue)(oListItem["CISite"])).Url; Console.WriteLine(hLink); ClientContext Context = new ClientContext(hLink); Context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; Context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("CRM Automation", "9LEkTny4"); Context.ExecuteQuery(); SP.List oListData = Context.Web.Lists.GetByTitle("Kursuspakke"); camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>"; ListItemCollection oListDataItem = oListData.GetItems(camlQuery); Context.Load(oListDataItem); Context.ExecuteQuery(); foreach (ListItem oItem in oListDataItem) { Console.WriteLine("ID: {0} \nTitle: {1} \nKursuspakkeUK:{2} \nOffered: {3} \nPrice:{4}", oItem.Id, oItem["Title"], oItem["KursuspakkeUK"], oItem["Offered"], oItem["Price"]); Console.WriteLine("IncludedInPriceAdditional: {0} \nIncludedInPriceDefault:{1} \nOptionalPurchases: {2} \nPricePerYear:{3}", oItem["IncludedInPriceAdditional"], oItem["IncludedInPriceDefault"], oItem["OptionalPurchases"], oItem["PricePerYear"]); Console.WriteLine(((SP.FieldUserValue)(oItem["Author"])).LookupValue); Console.WriteLine(((SP.FieldUserValue)(oItem["Editor"])).LookupValue); Console.WriteLine(oItem["Created"].ToString()); Console.WriteLine(oItem["Modified"].ToString()); Console.WriteLine(oItem["KursuspakkeID"]); CoursePackage coursePackage = dbContext.CoursePackage.Where(p => p.CoursePackageName == oItem["Title"].ToString()).First(); PartnerCoursePackage partnerCoursePackage = new PartnerCoursePackage(); PartnerPackageYear partnerPackageYear = new PartnerPackageYear(); // partnerCoursePackage.CRMPartnerId = oListItem.Id; partnerCoursePackage.CRMPartnerId = 1; //hard-coded value for test purpose partnerCoursePackage.CoursePackageId = coursePackage.CoursePackageId; // partnerCoursePackage.CoursePackageId = oItem.Id; partnerCoursePackage.ContentStatusId = 1; //hard-coded value for test purpose partnerCoursePackage.Offered = Convert.ToBoolean(oItem["Offered"]); partnerCoursePackage.Price = Convert.ToDecimal(oItem["Price"]); partnerCoursePackage.CreatedDate = Convert.ToDateTime(oItem["Created"].ToString()); partnerCoursePackage.LastModified = Convert.ToDateTime(oItem["Modified"].ToString()); partnerCoursePackage.LastModifiedBy = ((SP.FieldUserValue)(oItem["Editor"])).LookupValue; partnerCoursePackage.CreatedBy = ((SP.FieldUserValue)(oItem["Author"])).LookupValue; partnerPackageYear.PartnerCoursePackageId = oItem.Id; // if(oItem["PricePerYear"] != null) partnerPackageYear.Year = Convert.ToInt32(oItem["PricePerYear"]); partnerPackageYear.CreatedDate = Convert.ToDateTime(oItem["Created"].ToString()); partnerPackageYear.LastModified = Convert.ToDateTime(oItem["Modified"].ToString()); partnerPackageYear.LastModifiedBy = ((SP.FieldUserValue)(oItem["Editor"])).LookupValue; partnerPackageYear.CreatedBy = ((SP.FieldUserValue)(oItem["Author"])).LookupValue; //partnerPackageYear.PricePerPerson = oItem. if (oItem["IncludedInPriceDefault"] != null) { ArrayList parsedData = new ArrayList(); partnerCoursePackage.PartnerPackageIncludedItems = new List <PartnerPackageIncludedItem>(); SharedFunctions sharedFunctions = new SharedFunctions(); parsedData = sharedFunctions.ParseFunction(oItem["IncludedInPriceDefault"].ToString()); if (parsedData.Count > 0) { foreach (List <String> pacakageData in parsedData) { Console.WriteLine(pacakageData); Array pacakageDataArray = pacakageData.ToArray(); PackageIncludedItem packageIncludedItem = dbContext.PackageIncludedItem.Where(p => p.DK == pacakageData[0]).First(); partnerCoursePackage.PartnerPackageIncludedItems.Add(new PartnerPackageIncludedItem() { PartnerCoursePackageId = oItem.Id, PartnerPackageIncludedItemId = packageIncludedItem.PackageIncludedItemId, Offered = (pacakageData.Count > 2 ? (pacakageData[2] == "checked" ? true : false) : false) }); } } } //else //{ // partnerCoursePackage.PartnerPackageIncludedItems.Add(new PartnerPackageIncludedItems() { CoursePackageId = Convert.ToInt32(oItem["ID"]) }); //} if (oItem["IncludedInPriceAdditional"] != null) { ArrayList parsedData = new ArrayList(); partnerCoursePackage.PartnerPackageIncludedItems = new List <PartnerPackageIncludedItem>(); SharedFunctions sharedFunctions = new SharedFunctions(); parsedData = sharedFunctions.ParseFunction(oItem["IncludedInPriceAdditional"].ToString()); if (parsedData.Count > 0) { foreach (List <String> pacakageData in parsedData) { Console.WriteLine(pacakageData); Array pacakageDataArray = pacakageData.ToArray(); partnerCoursePackage.PartnerPackageAdditionalItems.Add(new PartnerPackageAdditionalItem() { PartnerCoursePackageId = oItem.Id, DK = pacakageData[0], CreatedDate = Convert.ToDateTime(oItem["Created"].ToString()), LastModified = Convert.ToDateTime(oItem["Modified"].ToString()), LastModifiedBy = ((SP.FieldUserValue)(oItem["Editor"])).LookupValue, CreatedBy = ((SP.FieldUserValue)(oItem["Author"])).LookupValue, Price = Convert.ToDecimal(oItem["Price"]) }); } } } if (oItem["OptionalPurchases"] != null) { ArrayList parsedData = new ArrayList(); partnerCoursePackage.PartnerPackageIncludedItems = new List <PartnerPackageIncludedItem>(); SharedFunctions sharedFunctions = new SharedFunctions(); parsedData = sharedFunctions.ParseFunction(oItem["OptionalPurchases"].ToString()); if (parsedData.Count > 0) { foreach (List <String> pacakageData in parsedData) { Console.WriteLine(pacakageData); Array pacakageDataArray = pacakageData.ToArray(); partnerCoursePackage.PartnerPackageAdditionalItems.Add(new PartnerPackageAdditionalItem() { PartnerCoursePackageId = oItem.Id, DK = pacakageData[0], CreatedDate = Convert.ToDateTime(oItem["Created"].ToString()), LastModified = Convert.ToDateTime(oItem["Modified"].ToString()), LastModifiedBy = ((SP.FieldUserValue)(oItem["Editor"])).LookupValue, CreatedBy = ((SP.FieldUserValue)(oItem["Author"])).LookupValue //Price = pacakageData[2] == "" ? 0 : Convert.ToDecimal(pacakageData[2]) }); } } } dbContext.Add(partnerCoursePackage); dbContext.SaveChanges(); } } } } catch (Exception) { throw; } }
public void InsertData(ClientContext clientContext, DKBSDbContext dbContext) { try { Console.WriteLine(" Successfully Connected"); SP.List oListData = clientContext.Web.Lists.GetByTitle("Service katalog"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>"; ListItemCollection oListDataItem = oListData.GetItems(camlQuery); SharedFunctions sharedFunctions = new SharedFunctions(); clientContext.Load(oListDataItem); clientContext.ExecuteQuery(); foreach (ListItem oItem in oListDataItem) { Console.WriteLine("ID: {0} \nTitle: {1}", oItem["ID"], oItem["Title"]); Console.WriteLine(((SP.FieldUserValue)(oItem["Author"])).LookupValue); Console.WriteLine(((SP.FieldUserValue)(oItem["Editor"])).LookupValue); Console.WriteLine(oItem["Created"].ToString()); Console.WriteLine(oItem["Modified"].ToString()); Console.WriteLine(oItem["KursuspakkeUK"]); Console.WriteLine(oItem["Offered"]); Console.WriteLine(oItem["Price"]); Console.WriteLine(oItem["KursuspakkeID"]); Console.WriteLine(oItem["IncludedInPriceDefault"]); Console.WriteLine(oItem["IncludedInPriceAdditional"]); Console.WriteLine(oItem["OptionalPurchases"]); var coursepack = new CoursePackage(); coursepack.CoursePackageName = oItem["Title"].ToString(); coursepack.CreatedDate = Convert.ToDateTime(oItem["Created"].ToString()); coursepack.LastModified = Convert.ToDateTime(oItem["Modified"].ToString()); //coursepack.Offered = oItem["Offered"].ToString(); coursepack.Price = Convert.ToDecimal(oItem["Price"]); //coursepack.CreatedBy = oItem["Author"].ToString(); // coursepack.LastModifiedBy = oItem["Editor"].ToString(); coursepack.CreatedBy = (((SP.FieldUserValue)(oItem["Author"])).LookupValue).ToString(); coursepack.LastModifiedBy = (((SP.FieldUserValue)(oItem["Editor"])).LookupValue).ToString(); ArrayList parsedData = new ArrayList(); coursepack.PackageIncludedItems = new List <PackageIncludedItem>(); if (oItem["IncludedInPriceDefault"] != null) { parsedData = sharedFunctions.ParseFunction(oItem["IncludedInPriceDefault"].ToString()); if (parsedData.Count > 0) { foreach (List <String> pacakageData in parsedData) { Console.WriteLine(pacakageData); Array pacakageDataArray = pacakageData.ToArray(); coursepack.PackageIncludedItems.Add(new PackageIncludedItem() { CoursePackageId = Convert.ToInt32(oItem["ID"]), DK = pacakageData[0].ToString() }); } } } else { coursepack.PackageIncludedItems.Add(new PackageIncludedItem() { CoursePackageId = Convert.ToInt32(oItem["ID"]) }); } dbContext.Add(coursepack); dbContext.SaveChanges(); //if (oItem["KursuspakkeTypen"] != null) //{ // Console.WriteLine(oItem["KursuspakkeTypen"]); // var childIdField = oItem["KursuspakkeTypen"] as FieldLookupValue[]; // if (childIdField != null) // { // foreach (var lookupValue in childIdField) // { // var childId_Value = lookupValue.LookupValue; // var childId_Id = lookupValue.LookupId; // Console.WriteLine("LookupID: " + childId_Id.ToString()); // Console.WriteLine("LookupValue: " + childId_Value.ToString()); // } // } //} } } catch (Exception) { throw; } }