Beispiel #1
0
        public static FieldUpdateValue ParseFieldValue(Web web, string fieldValue, Field dataField)
        {
            switch (dataField.FieldTypeKind)
            {
            case FieldType.Geolocation:
                // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                var geolocationArray = fieldValue.Split(',');
                if (geolocationArray.Length == 4)
                {
                    var geolocationValue = new FieldGeolocationValue
                    {
                        Altitude  = Double.Parse(geolocationArray[0]),
                        Latitude  = Double.Parse(geolocationArray[1]),
                        Longitude = Double.Parse(geolocationArray[2]),
                        Measure   = Double.Parse(geolocationArray[3]),
                    };
                    return(new FieldUpdateValue(dataField.InternalName, geolocationValue));
                }
                else
                {
                    return(new FieldUpdateValue(dataField.InternalName, fieldValue));
                }

            case FieldType.Lookup:
                if (dataField.TypeAsString == "LookupMulti" && TryDeserializeAsJson(fieldValue, out FieldLookupValue[] lookupValues2))
        /// <summary>
        /// Add list items to a list.
        /// </summary>
        /// <param name="web">Web where the list exists.</param>
        /// <param name="list">List to add items to.</param>
        /// <param name="dataRows">XML configration for items to add.</param>
        /// <param name="tokenParser"></param>
        private static void AddItemsToList(this Web web, List list, IEnumerable <XElement> dataRows, TokenParser tokenParser)
        {
            // Retrieve the fields' types from the list
            FieldCollection fields = list.Fields;

            web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
            web.Context.ExecuteQueryRetry();

            foreach (var dataRow in dataRows)
            {
                try
                {
                    var listitemCI = new ListItemCreationInformation();
                    var listitem   = list.AddItem(listitemCI);

                    foreach (var dataValue in dataRow.Attributes())
                    {
                        Field dataField = fields.FirstOrDefault(
                            f => f.InternalName == dataValue.Name.LocalName);

                        if (dataField != null)
                        {
                            String fieldValue = tokenParser.ParseString(dataValue.Value);

                            switch (dataField.FieldTypeKind)
                            {
                            case FieldType.Geolocation:
                                // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                var geolocationArray = fieldValue.Split(',');
                                if (geolocationArray.Length == 4)
                                {
                                    var geolocationValue = new FieldGeolocationValue
                                    {
                                        Altitude  = Double.Parse(geolocationArray[0]),
                                        Latitude  = Double.Parse(geolocationArray[1]),
                                        Longitude = Double.Parse(geolocationArray[2]),
                                        Measure   = Double.Parse(geolocationArray[3]),
                                    };
                                    listitem[dataValue.Name.LocalName] = geolocationValue;
                                }
                                else
                                {
                                    listitem[dataValue.Name.LocalName] = fieldValue;
                                }
                                break;

                            case FieldType.Lookup:
                                // FieldLookupValue - Expected format: LookupID
                                var lookupValue = new FieldLookupValue
                                {
                                    LookupId = Int32.Parse(fieldValue),
                                };
                                listitem[dataValue.Name.LocalName] = lookupValue;
                                break;

                            case FieldType.URL:
                                // FieldUrlValue - Expected format: URL,Description
                                var urlArray  = fieldValue.Split(',');
                                var linkValue = new FieldUrlValue();
                                if (urlArray.Length == 2)
                                {
                                    linkValue.Url         = urlArray[0];
                                    linkValue.Description = urlArray[1];
                                }
                                else
                                {
                                    linkValue.Url         = urlArray[0];
                                    linkValue.Description = urlArray[0];
                                }
                                listitem[dataValue.Name.LocalName] = linkValue;
                                break;

                            case FieldType.User:
                                // FieldUserValue - Expected format: loginName
                                var user = web.EnsureUser(fieldValue);
                                web.Context.Load(user);
                                web.Context.ExecuteQueryRetry();

                                if (user != null)
                                {
                                    var userValue = new FieldUserValue
                                    {
                                        LookupId = user.Id,
                                    };
                                    listitem[dataValue.Name.LocalName] = userValue;
                                }
                                else
                                {
                                    listitem[dataValue.Name.LocalName] = fieldValue;
                                }
                                break;

                            case FieldType.Invalid:
                                if (dataField.GetType() == typeof(Microsoft.SharePoint.Client.Taxonomy.TaxonomyField))
                                {
                                    var txField = web.Context.CastTo <TaxonomyField>(dataField);
                                    web.Context.Load(txField, tx => tx.TermSetId);
                                    web.Context.ExecuteQueryRetry();

                                    var ts      = TaxonomySession.GetTaxonomySession(web.Context);
                                    var termSet = ts.GetDefaultSiteCollectionTermStore().GetTermSet(txField.TermSetId);
                                    var term    = termSet.Terms.GetByName(fieldValue);
                                    web.Context.Load(term, t => t.Id);
                                    web.Context.ExecuteQueryRetry();

                                    var termValue = new TaxonomyFieldValue();
                                    termValue.TermGuid = term.Id.ToString();
                                    txField.SetFieldValueByValue(listitem, termValue);
                                }
                                else
                                {
                                    listitem[dataValue.Name.LocalName] = fieldValue;
                                }
                                break;

                            default:
                                listitem[dataValue.Name.LocalName] = fieldValue;
                                break;
                            }
                        }
                        listitem.Update();
                    }
                    web.Context.ExecuteQueryRetry(); // TODO: Run in batches?
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {

                if (template.Lists.Any())
                {
                    var rootWeb = (web.Context as ClientContext).Site.RootWeb;

                    web.EnsureProperties(w => w.ServerRelativeUrl);

                    web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
                    web.Context.ExecuteQueryRetry();
                    var existingLists = web.Lists.AsEnumerable<List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
                    var serverRelativeUrl = web.ServerRelativeUrl;

                    #region DataRows

                    foreach (var listInstance in template.Lists)
                    {
                        if (listInstance.DataRows != null && listInstance.DataRows.Any())
                        {
                            scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Processing_data_rows_for__0_, listInstance.Title);
                            // Retrieve the target list
                            var list = web.Lists.GetByTitle(listInstance.Title);
                            web.Context.Load(list);

                            // Retrieve the fields' types from the list
                            FieldCollection fields = list.Fields;
                            web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
                            web.Context.ExecuteQueryRetry();

                            foreach (var dataRow in listInstance.DataRows)
                            {
                                try
                                {
                                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_list_item__0_, listInstance.DataRows.IndexOf(dataRow) + 1);
                                    var listitemCI = new ListItemCreationInformation();
                                    var listitem = list.AddItem(listitemCI);

                                    foreach (var dataValue in dataRow.Values)
                                    {
                                        Field dataField = fields.FirstOrDefault(
                                            f => f.InternalName == parser.ParseString(dataValue.Key));

                                        if (dataField != null)
                                        {
                                            String fieldValue = parser.ParseString(dataValue.Value);

                                            switch (dataField.FieldTypeKind)
                                            {
                                                case FieldType.Geolocation:
                                                    // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                                    var geolocationArray = fieldValue.Split(',');
                                                    if (geolocationArray.Length == 4)
                                                    {
                                                        var geolocationValue = new FieldGeolocationValue
                                                        {
                                                            Altitude = Double.Parse(geolocationArray[0]),
                                                            Latitude = Double.Parse(geolocationArray[1]),
                                                            Longitude = Double.Parse(geolocationArray[2]),
                                                            Measure = Double.Parse(geolocationArray[3]),
                                                        };
                                                        listitem[parser.ParseString(dataValue.Key)] = geolocationValue;
                                                    }
                                                    else
                                                    {
                                                        listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                    }
                                                    break;
                                                case FieldType.Lookup:
                                                    // FieldLookupValue - Expected format: LookupID
                                                    var lookupValue = new FieldLookupValue
                                                    {
                                                        LookupId = Int32.Parse(fieldValue),
                                                    };
                                                    listitem[parser.ParseString(dataValue.Key)] = lookupValue;
                                                    break;
                                                case FieldType.URL:
                                                    // FieldUrlValue - Expected format: URL,Description
                                                    var urlArray = fieldValue.Split(',');
                                                    var linkValue = new FieldUrlValue();
                                                    if (urlArray.Length == 2)
                                                    {
                                                        linkValue.Url = urlArray[0];
                                                        linkValue.Description = urlArray[1];
                                                    }
                                                    else
                                                    {
                                                        linkValue.Url = urlArray[0];
                                                        linkValue.Description = urlArray[0];
                                                    }
                                                    listitem[parser.ParseString(dataValue.Key)] = linkValue;
                                                    break;
                                                case FieldType.User:
                                                    // FieldUserValue - Expected format: loginName
                                                    var user = web.EnsureUser(fieldValue);
                                                    web.Context.Load(user);
                                                    web.Context.ExecuteQueryRetry();

                                                    if (user != null)
                                                    {
                                                        var userValue = new FieldUserValue
                                                        {
                                                            LookupId = user.Id,
                                                        };
                                                        listitem[parser.ParseString(dataValue.Key)] = userValue;
                                                    }
                                                    else
                                                    {
                                                        listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                    }
                                                    break;
                                                default:
                                                    listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                    break;
                                            }
                                        }
                                        listitem.Update();
                                    }
                                    web.Context.ExecuteQueryRetry(); // TODO: Run in batches?

                                    if (dataRow.Security != null)
                                    {
                                        listitem.SetSecurity(parser, dataRow.Security);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    scope.LogError(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_listitem_failed___0_____1_, ex.Message, ex.StackTrace);
                                    throw;
                                }
                            }
                        }
                    }

                    #endregion
                }
            }

            return parser;
        }
Beispiel #4
0
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (template.Lists.Any())
                {
                    var rootWeb = (web.Context as ClientContext).Site.RootWeb;

                    web.EnsureProperties(w => w.ServerRelativeUrl);

                    web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
                    web.Context.ExecuteQueryRetry();
                    var existingLists     = web.Lists.AsEnumerable <List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
                    var serverRelativeUrl = web.ServerRelativeUrl;

                    #region DataRows

                    foreach (var listInstance in template.Lists)
                    {
                        if (listInstance.DataRows != null && listInstance.DataRows.Any())
                        {
                            scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Processing_data_rows_for__0_, listInstance.Title);
                            // Retrieve the target list
                            var list = web.Lists.GetByTitle(listInstance.Title);
                            web.Context.Load(list);

                            // Retrieve the fields' types from the list
                            Microsoft.SharePoint.Client.FieldCollection fields = list.Fields;
                            web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
                            web.Context.ExecuteQueryRetry();

                            foreach (var dataRow in listInstance.DataRows)
                            {
                                try
                                {
                                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_list_item__0_, listInstance.DataRows.IndexOf(dataRow) + 1);
                                    var listitemCI = new ListItemCreationInformation();
                                    var listitem   = list.AddItem(listitemCI);

                                    foreach (var dataValue in dataRow.Values)
                                    {
                                        Field dataField = fields.FirstOrDefault(
                                            f => f.InternalName == parser.ParseString(dataValue.Key));

                                        if (dataField != null)
                                        {
                                            String fieldValue = parser.ParseString(dataValue.Value);

                                            switch (dataField.FieldTypeKind)
                                            {
                                            case FieldType.Geolocation:
                                                // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                                var geolocationArray = fieldValue.Split(',');
                                                if (geolocationArray.Length == 4)
                                                {
                                                    var geolocationValue = new FieldGeolocationValue
                                                    {
                                                        Altitude  = Double.Parse(geolocationArray[0]),
                                                        Latitude  = Double.Parse(geolocationArray[1]),
                                                        Longitude = Double.Parse(geolocationArray[2]),
                                                        Measure   = Double.Parse(geolocationArray[3]),
                                                    };
                                                    listitem[parser.ParseString(dataValue.Key)] = geolocationValue;
                                                }
                                                else
                                                {
                                                    listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                }
                                                break;

                                            case FieldType.Lookup:
                                                // FieldLookupValue - Expected format: LookupID or LookupID,LookupID,LookupID...
                                                if (fieldValue.Contains(","))
                                                {
                                                    var lookupValues = new List <FieldLookupValue>();
                                                    fieldValue.Split(',').All(value =>
                                                    {
                                                        lookupValues.Add(new FieldLookupValue
                                                        {
                                                            LookupId = int.Parse(value),
                                                        });
                                                        return(true);
                                                    });
                                                    listitem[parser.ParseString(dataValue.Key)] = lookupValues.ToArray();
                                                }
                                                else
                                                {
                                                    var lookupValue = new FieldLookupValue
                                                    {
                                                        LookupId = int.Parse(fieldValue),
                                                    };
                                                    listitem[parser.ParseString(dataValue.Key)] = lookupValue;
                                                }
                                                break;

                                            case FieldType.URL:
                                                // FieldUrlValue - Expected format: URL,Description
                                                var urlArray  = fieldValue.Split(',');
                                                var linkValue = new FieldUrlValue();
                                                if (urlArray.Length == 2)
                                                {
                                                    linkValue.Url         = urlArray[0];
                                                    linkValue.Description = urlArray[1];
                                                }
                                                else
                                                {
                                                    linkValue.Url         = urlArray[0];
                                                    linkValue.Description = urlArray[0];
                                                }
                                                listitem[parser.ParseString(dataValue.Key)] = linkValue;
                                                break;

                                            case FieldType.User:
                                                // FieldUserValue - Expected format: loginName or loginName,loginName,loginName...
                                                if (fieldValue.Contains(","))
                                                {
                                                    var userValues = new List <FieldUserValue>();
                                                    fieldValue.Split(',').All(value =>
                                                    {
                                                        var user = web.EnsureUser(value);
                                                        web.Context.Load(user);
                                                        web.Context.ExecuteQueryRetry();
                                                        if (user != null)
                                                        {
                                                            userValues.Add(new FieldUserValue
                                                            {
                                                                LookupId = user.Id,
                                                            });;
                                                        }
                                                        return(true);
                                                    });
                                                    listitem[parser.ParseString(dataValue.Key)] = userValues.ToArray();
                                                }
                                                else
                                                {
                                                    var user = web.EnsureUser(fieldValue);
                                                    web.Context.Load(user);
                                                    web.Context.ExecuteQueryRetry();
                                                    if (user != null)
                                                    {
                                                        var userValue = new FieldUserValue
                                                        {
                                                            LookupId = user.Id,
                                                        };
                                                        listitem[parser.ParseString(dataValue.Key)] = userValue;
                                                    }
                                                    else
                                                    {
                                                        listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                    }
                                                }
                                                break;

                                            case FieldType.DateTime:
                                                var dateTime = DateTime.MinValue;
                                                if (DateTime.TryParse(fieldValue, out dateTime))
                                                {
                                                    listitem[parser.ParseString(dataValue.Key)] = dateTime;
                                                }
                                                break;

                                            default:
                                                listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                break;
                                            }
                                        }
                                        listitem.Update();
                                    }
                                    web.Context.ExecuteQueryRetry();                                     // TODO: Run in batches?

                                    if (dataRow.Security != null && dataRow.Security.RoleAssignments.Count != 0)
                                    {
                                        listitem.SetSecurity(parser, dataRow.Security);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    scope.LogError(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_listitem_failed___0_____1_, ex.Message, ex.StackTrace);
                                    throw;
                                }
                            }
                        }
                    }

                    #endregion
                }
            }

            return(parser);
        }
Beispiel #5
0
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (template.Lists.Any())
                {
                    var rootWeb = (web.Context as ClientContext).Site.RootWeb;

                    web.EnsureProperties(w => w.ServerRelativeUrl);

                    web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
                    web.Context.ExecuteQueryRetry();
                    var existingLists     = web.Lists.AsEnumerable <List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
                    var serverRelativeUrl = web.ServerRelativeUrl;

                    #region DataRows

                    foreach (var listInstance in template.Lists)
                    {
                        if (listInstance.DataRows != null && listInstance.DataRows.Any())
                        {
                            scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Processing_data_rows_for__0_, listInstance.Title);
                            // Retrieve the target list
                            var list = web.Lists.GetByTitle(parser.ParseString(listInstance.Title));
                            web.Context.Load(list);

                            // Retrieve the fields' types from the list
                            Microsoft.SharePoint.Client.FieldCollection fields = list.Fields;
                            web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind, f => f.TypeAsString));
                            web.Context.ExecuteQueryRetry();

                            var keyColumnType   = "Text";
                            var parsedKeyColumn = parser.ParseString(listInstance.DataRows.KeyColumn);
                            if (!string.IsNullOrEmpty(parsedKeyColumn))
                            {
                                var keyColumn = fields.FirstOrDefault(f => f.InternalName.Equals(parsedKeyColumn, StringComparison.InvariantCultureIgnoreCase));
                                if (keyColumn != null)
                                {
                                    switch (keyColumn.FieldTypeKind)
                                    {
                                    case FieldType.User:
                                    case FieldType.Lookup:
                                        keyColumnType = "Lookup";
                                        break;

                                    case FieldType.URL:
                                        keyColumnType = "Url";
                                        break;

                                    case FieldType.DateTime:
                                        keyColumnType = "DateTime";
                                        break;

                                    case FieldType.Number:
                                    case FieldType.Counter:
                                        keyColumnType = "Number";
                                        break;
                                    }
                                }
                            }

                            foreach (var dataRow in listInstance.DataRows)
                            {
                                try
                                {
                                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_list_item__0_, listInstance.DataRows.IndexOf(dataRow) + 1);


                                    bool     create   = true;
                                    ListItem listitem = null;
                                    if (!string.IsNullOrEmpty(listInstance.DataRows.KeyColumn))
                                    {
                                        // Get value from key column
                                        var dataRowValues = dataRow.Values.Where(v => v.Key == listInstance.DataRows.KeyColumn);

                                        // if it is empty, skip the check
                                        if (dataRowValues.Any())
                                        {
                                            var query     = $@"<View><Query><Where><Eq><FieldRef Name=""{parsedKeyColumn}""/><Value Type=""{keyColumnType}"">{dataRowValues.FirstOrDefault().Value}</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>";
                                            var camlQuery = new CamlQuery()
                                            {
                                                ViewXml = query
                                            };
                                            var existingItems = list.GetItems(camlQuery);
                                            list.Context.Load(existingItems);
                                            list.Context.ExecuteQueryRetry();
                                            if (existingItems.Count > 0)
                                            {
                                                if (listInstance.DataRows.UpdateBehavior == UpdateBehavior.Skip)
                                                {
                                                    create = false;
                                                }
                                                else
                                                {
                                                    listitem = existingItems[0];
                                                    create   = true;
                                                }
                                            }
                                        }
                                    }

                                    if (create)
                                    {
                                        if (listitem == null)
                                        {
                                            var listitemCI = new ListItemCreationInformation();
                                            listitem = list.AddItem(listitemCI);
                                        }

                                        foreach (var dataValue in dataRow.Values)
                                        {
                                            Field dataField = fields.FirstOrDefault(
                                                f => f.InternalName == parser.ParseString(dataValue.Key));

                                            if (dataField != null)
                                            {
                                                String fieldValue = parser.ParseString(dataValue.Value);

                                                switch (dataField.FieldTypeKind)
                                                {
                                                case FieldType.Geolocation:
                                                    // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                                    var geolocationArray = fieldValue.Split(',');
                                                    if (geolocationArray.Length == 4)
                                                    {
                                                        var geolocationValue = new FieldGeolocationValue
                                                        {
                                                            Altitude  = Double.Parse(geolocationArray[0]),
                                                            Latitude  = Double.Parse(geolocationArray[1]),
                                                            Longitude = Double.Parse(geolocationArray[2]),
                                                            Measure   = Double.Parse(geolocationArray[3]),
                                                        };
                                                        listitem[parser.ParseString(dataValue.Key)] = geolocationValue;
                                                    }
                                                    else
                                                    {
                                                        listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                    }
                                                    break;

                                                case FieldType.Lookup:
                                                    // FieldLookupValue - Expected format: LookupID or LookupID,LookupID,LookupID...
                                                    if (fieldValue.Contains(","))
                                                    {
                                                        var lookupValues = new List <FieldLookupValue>();
                                                        fieldValue.Split(',').All(value =>
                                                        {
                                                            lookupValues.Add(new FieldLookupValue
                                                            {
                                                                LookupId = int.Parse(value),
                                                            });
                                                            return(true);
                                                        });
                                                        listitem[parser.ParseString(dataValue.Key)] = lookupValues.ToArray();
                                                    }
                                                    else
                                                    {
                                                        var lookupValue = new FieldLookupValue
                                                        {
                                                            LookupId = int.Parse(fieldValue),
                                                        };
                                                        listitem[parser.ParseString(dataValue.Key)] = lookupValue;
                                                    }
                                                    break;

                                                case FieldType.URL:
                                                    // FieldUrlValue - Expected format: URL,Description
                                                    var urlArray  = fieldValue.Split(',');
                                                    var linkValue = new FieldUrlValue();
                                                    if (urlArray.Length == 2)
                                                    {
                                                        linkValue.Url         = urlArray[0];
                                                        linkValue.Description = urlArray[1];
                                                    }
                                                    else
                                                    {
                                                        linkValue.Url         = urlArray[0];
                                                        linkValue.Description = urlArray[0];
                                                    }
                                                    listitem[parser.ParseString(dataValue.Key)] = linkValue;
                                                    break;

                                                case FieldType.User:
                                                    // FieldUserValue - Expected format: loginName or loginName,loginName,loginName...
                                                    if (fieldValue.Contains(","))
                                                    {
                                                        var userValues = new List <FieldUserValue>();
                                                        fieldValue.Split(',').All(value =>
                                                        {
                                                            var user = web.EnsureUser(value);
                                                            web.Context.Load(user);
                                                            web.Context.ExecuteQueryRetry();
                                                            if (user != null)
                                                            {
                                                                userValues.Add(new FieldUserValue
                                                                {
                                                                    LookupId = user.Id,
                                                                });;
                                                            }
                                                            return(true);
                                                        });
                                                        listitem[parser.ParseString(dataValue.Key)] = userValues.ToArray();
                                                    }
                                                    else
                                                    {
                                                        var user = web.EnsureUser(fieldValue);
                                                        web.Context.Load(user);
                                                        web.Context.ExecuteQueryRetry();
                                                        if (user != null)
                                                        {
                                                            var userValue = new FieldUserValue
                                                            {
                                                                LookupId = user.Id,
                                                            };
                                                            listitem[parser.ParseString(dataValue.Key)] = userValue;
                                                        }
                                                        else
                                                        {
                                                            listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                        }
                                                    }
                                                    break;

                                                case FieldType.DateTime:
                                                    var dateTime = DateTime.MinValue;
                                                    if (DateTime.TryParse(fieldValue, out dateTime))
                                                    {
                                                        listitem[parser.ParseString(dataValue.Key)] = dateTime;
                                                    }
                                                    break;

                                                default:
                                                    listitem[parser.ParseString(dataValue.Key)] = fieldValue;
                                                    break;
                                                }
                                                listitem.Update();
                                            }
                                        }
                                        web.Context.ExecuteQueryRetry(); // TODO: Run in batches?

                                        if (dataRow.Security != null && (dataRow.Security.ClearSubscopes == true || dataRow.Security.CopyRoleAssignments == true || dataRow.Security.RoleAssignments.Count > 0))
                                        {
                                            listitem.SetSecurity(parser, dataRow.Security);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    if (ex.GetType().Equals(typeof(ServerException)) &&
                                        (ex as ServerException).ServerErrorTypeName.Equals("Microsoft.SharePoint.SPDuplicateValuesFoundException", StringComparison.InvariantCultureIgnoreCase) &&
                                        applyingInformation.IgnoreDuplicateDataRowErrors)
                                    {
                                        scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_listitem_duplicate);
                                        continue;
                                    }
                                    else
                                    {
                                        scope.LogError(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_listitem_failed___0_____1_, ex.Message, ex.StackTrace);
                                        throw;
                                    }
                                }
                            }
                        }
                    }

                    #endregion
                }
            }

            return(parser);
        }
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (!template.Lists.Any())
                {
                    return(parser);
                }

                web.EnsureProperties(w => w.ServerRelativeUrl);

                web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
                web.Context.ExecuteQueryRetry();

                #region DataRows
                foreach (var listInstance in template.Lists)
                {
                    if (listInstance.DataRows == null || !listInstance.DataRows.Any())
                    {
                        continue;
                    }

                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Processing_data_rows_for__0_, listInstance.Title);
                    // Retrieve the target list
                    var list = web.Lists.GetByTitle(parser.ParseString(listInstance.Title));
                    web.Context.Load(list);

                    // Retrieve the fields' types from the list
                    Microsoft.SharePoint.Client.FieldCollection fields = list.Fields;
                    web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind, f => f.TypeAsString, f => f.ReadOnlyField, f => f.Title));
                    web.Context.ExecuteQueryRetry();

                    var keyColumnType   = "Text";
                    var parsedKeyColumn = parser.ParseString(listInstance.DataRows.KeyColumn);
                    if (!string.IsNullOrEmpty(parsedKeyColumn))
                    {
                        var keyColumn = fields.FirstOrDefault(f => f.InternalName.Equals(parsedKeyColumn, StringComparison.InvariantCultureIgnoreCase));
                        if (keyColumn != null)
                        {
                            switch (keyColumn.FieldTypeKind)
                            {
                            case FieldType.User:
                            case FieldType.Lookup:
                                keyColumnType = "Lookup";
                                break;

                            case FieldType.URL:
                                keyColumnType = "Url";
                                break;

                            case FieldType.DateTime:
                                keyColumnType = "DateTime";
                                break;

                            case FieldType.Number:
                            case FieldType.Counter:
                                keyColumnType = "Number";
                                break;
                            }
                        }
                    }

                    foreach (var dataRow in listInstance.DataRows)
                    {
                        try
                        {
                            scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_list_item__0_, listInstance.DataRows.IndexOf(dataRow) + 1);


                            bool     processItem  = true;
                            ListItem listitem     = null;
                            var      updateValues = new List <FieldUpdateValue>();

                            if (!string.IsNullOrEmpty(listInstance.DataRows.KeyColumn))
                            {
                                // Get value from key column
                                var dataRowValues = dataRow.Values.Where(v => v.Key == listInstance.DataRows.KeyColumn).ToList();

                                // if it is empty, skip the check
                                if (dataRowValues.Any())
                                {
                                    var query     = $@"<View><Query><Where><Eq><FieldRef Name=""{parsedKeyColumn}""/><Value Type=""{keyColumnType}"">{parser.ParseString(dataRowValues.FirstOrDefault().Value)}</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>";
                                    var camlQuery = new CamlQuery()
                                    {
                                        ViewXml = query
                                    };
                                    var existingItems = list.GetItems(camlQuery);
                                    list.Context.Load(existingItems);
                                    list.Context.ExecuteQueryRetry();
                                    if (existingItems.Count > 0)
                                    {
                                        if (listInstance.DataRows.UpdateBehavior == UpdateBehavior.Skip)
                                        {
                                            processItem = false;
                                        }
                                        else
                                        {
                                            listitem    = existingItems[0];
                                            processItem = true;
                                        }
                                    }
                                }
                            }

                            if (!processItem)
                            {
                                continue;
                            }

                            if (listitem == null)
                            {
                                var listitemCI = new ListItemCreationInformation();
                                listitem = list.AddItem(listitemCI);
                            }

                            foreach (var dataValue in dataRow.Values)
                            {
                                Field dataField = fields.FirstOrDefault(
                                    f => f.InternalName == parser.ParseString(dataValue.Key));

                                if (dataField != null && dataField.ReadOnlyField &&
                                    !dataField.InternalName.Equals("ContentTypeId", StringComparison.OrdinalIgnoreCase))
                                {
                                    // skip read only fields, except ContentTypeId
                                    continue;
                                }

                                if (dataField == null)
                                {
                                    continue;
                                }

                                if (dataValue.Value == null)
                                {
                                    updateValues.Add(dataField.FieldTypeKind == FieldType.Invalid
                                        ? new FieldUpdateValue(dataValue.Key, null, dataField.TypeAsString)
                                        : new FieldUpdateValue(dataValue.Key, null));
                                }
                                else
                                {
                                    String fieldValue = parser.ParseString(dataValue.Value);

                                    switch (dataField.FieldTypeKind)
                                    {
                                    case FieldType.Geolocation:
                                        // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                        var geolocationArray = fieldValue.Split(',');
                                        if (geolocationArray.Length == 4)
                                        {
                                            var geolocationValue = new FieldGeolocationValue
                                            {
                                                Altitude  = Double.Parse(geolocationArray[0]),
                                                Latitude  = Double.Parse(geolocationArray[1]),
                                                Longitude = Double.Parse(geolocationArray[2]),
                                                Measure   = Double.Parse(geolocationArray[3]),
                                            };
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, geolocationValue));
                                        }
                                        else
                                        {
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, fieldValue));
                                        }
                                        break;

                                    case FieldType.Lookup:
                                        // FieldLookupValue - Expected format: LookupID or LookupID,LookupID,LookupID...
                                        if (fieldValue.Contains(","))
                                        {
                                            var lookupValues = new List <FieldLookupValue>();
                                            fieldValue.Split(',').All(value =>
                                            {
                                                lookupValues.Add(new FieldLookupValue
                                                {
                                                    LookupId = int.Parse(value),
                                                });
                                                return(true);
                                            });
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, lookupValues.ToArray()));
                                        }
                                        else
                                        {
                                            var lookupValue = new FieldLookupValue
                                            {
                                                LookupId = int.Parse(fieldValue),
                                            };
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, lookupValue));
                                        }
                                        break;

                                    case FieldType.URL:
                                        // FieldUrlValue - Expected format: URL,Description
                                        var urlArray  = fieldValue.Split(',');
                                        var linkValue = new FieldUrlValue();
                                        if (urlArray.Length == 2)
                                        {
                                            linkValue.Url         = urlArray[0];
                                            linkValue.Description = urlArray[1];
                                        }
                                        else
                                        {
                                            linkValue.Url         = urlArray[0];
                                            linkValue.Description = urlArray[0];
                                        }
                                        updateValues.Add(new FieldUpdateValue(dataValue.Key, linkValue));
                                        break;

                                    case FieldType.User:
                                        // FieldUserValue - Expected format: loginName or loginName,loginName,loginName...
                                        if (fieldValue.Contains(","))
                                        {
                                            var userValues = new List <FieldUserValue>();
                                            fieldValue.Split(',').All(value =>
                                            {
                                                var user = web.EnsureUser(value);
                                                web.Context.Load(user);
                                                web.Context.ExecuteQueryRetry();
                                                if (user != null)
                                                {
                                                    userValues.Add(new FieldUserValue
                                                    {
                                                        LookupId = user.Id,
                                                    });;
                                                }
                                                return(true);
                                            });
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, userValues.ToArray()));
                                        }
                                        else
                                        {
                                            var user = web.EnsureUser(fieldValue);
                                            web.Context.Load(user);
                                            web.Context.ExecuteQueryRetry();
                                            if (user != null)
                                            {
                                                var userValue = new FieldUserValue
                                                {
                                                    LookupId = user.Id,
                                                };
                                                updateValues.Add(new FieldUpdateValue(dataValue.Key, userValue));
                                            }
                                            else
                                            {
                                                updateValues.Add(new FieldUpdateValue(dataValue.Key, fieldValue));
                                            }
                                        }
                                        break;

                                    case FieldType.DateTime:
                                        var dateTime = DateTime.MinValue;
                                        if (DateTime.TryParse(fieldValue, out dateTime))
                                        {
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, dateTime));
                                        }
                                        break;

                                    case FieldType.Invalid:
                                        switch (dataField.TypeAsString)
                                        {
                                        case "TaxonomyFieldType":
                                        // Single value field - Expected format: term label|term GUID
                                        case "TaxonomyFieldTypeMulti":
                                            // Multi value field - Expected format: term label|term GUID;term label|term GUID;term label|term GUID;...
                                        {
                                            if (fieldValue != null)
                                            {
                                                var termStrings = new List <string>();

                                                var termsArray = fieldValue.Split(new char[] { ';' });
                                                foreach (var term in termsArray)
                                                {
                                                    termStrings.Add($"-1;#{term}");
                                                }
                                                updateValues.Add(new FieldUpdateValue(dataValue.Key, termStrings, dataField.TypeAsString));
                                            }
                                            break;
                                        }

                                        default:
                                        {
                                            //Publishing image case, but can be others too
                                            updateValues.Add(new FieldUpdateValue(dataValue.Key, fieldValue));
                                            break;
                                        }
                                        }
                                        break;

                                    default:
                                        updateValues.Add(new FieldUpdateValue(dataValue.Key, fieldValue));
                                        break;
                                    }
                                }
                            }

                            foreach (var itemValue in updateValues.Where(u => u.FieldTypeString != "TaxonomyFieldTypeMulti" && u.FieldTypeString != "TaxonomyFieldType"))
                            {
                                if (string.IsNullOrEmpty(itemValue.FieldTypeString))
                                {
                                    listitem[itemValue.Key] = itemValue.Value;
                                }
                            }
                            listitem.Update();
                            web.Context.Load(listitem);
                            web.Context.ExecuteQueryRetry();
                            foreach (var itemValue in updateValues.Where(u => u.FieldTypeString == "TaxonomyFieldTypeMulti" || u.FieldTypeString == "TaxonomyFieldType"))
                            {
                                switch (itemValue.FieldTypeString)
                                {
                                case "TaxonomyFieldTypeMulti":
                                {
                                    var field    = fields.FirstOrDefault(f => f.InternalName == itemValue.Key as string || f.Title == itemValue.Key as string);
                                    var taxField = web.Context.CastTo <TaxonomyField>(field);
                                    if (itemValue.Value != null)
                                    {
                                        var valueCollection = new TaxonomyFieldValueCollection(web.Context, string.Join(";#", itemValue.Value as List <string>), taxField);
                                        taxField.SetFieldValueByValueCollection(listitem, valueCollection);
                                    }
                                    else
                                    {
                                        var valueCollection = new TaxonomyFieldValueCollection(web.Context, null, taxField);
                                        taxField.SetFieldValueByValueCollection(listitem, valueCollection);
                                    }
                                    listitem.Update();
                                    web.Context.Load(listitem);
                                    web.Context.ExecuteQueryRetry();
                                    break;
                                }

                                case "TaxonomyFieldType":
                                {
                                    var field    = fields.FirstOrDefault(f => f.InternalName == itemValue.Key as string || f.Title == itemValue.Key as string);
                                    var taxField = web.Context.CastTo <TaxonomyField>(field);
                                    taxField.EnsureProperty(f => f.TextField);
                                    var taxValue = new TaxonomyFieldValue();
                                    if (itemValue.Value != null)
                                    {
                                        var termString = (itemValue.Value as List <string>).First();
                                        taxValue.Label    = termString.Split(new string[] { ";#" }, StringSplitOptions.None)[1].Split(new char[] { '|' })[0];
                                        taxValue.TermGuid = termString.Split(new string[] { ";#" }, StringSplitOptions.None)[1].Split(new char[] { '|' })[1];
                                        taxValue.WssId    = -1;
                                        taxField.SetFieldValueByValue(listitem, taxValue);
                                    }
                                    else
                                    {
                                        taxValue.Label    = string.Empty;
                                        taxValue.TermGuid = "11111111-1111-1111-1111-111111111111";
                                        taxValue.WssId    = -1;
                                        Field hiddenField = list.Fields.GetById(taxField.TextField);
                                        listitem.Context.Load(hiddenField, tf => tf.InternalName);
                                        listitem.Context.ExecuteQueryRetry();
                                        taxField.SetFieldValueByValue(listitem, taxValue);     // this order of updates is important.
                                        listitem[hiddenField.InternalName] = string.Empty;     // this order of updates is important.
                                    }
                                    listitem.Update();
                                    web.Context.Load(listitem);
                                    web.Context.ExecuteQueryRetry();
                                    break;
                                }
                                }
                            }
                            if (dataRow.Security != null && (dataRow.Security.ClearSubscopes == true || dataRow.Security.CopyRoleAssignments == true || dataRow.Security.RoleAssignments.Count > 0))
                            {
                                listitem.SetSecurity(parser, dataRow.Security);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex.GetType().Equals(typeof(ServerException)) &&
                                (ex as ServerException).ServerErrorTypeName.Equals("Microsoft.SharePoint.SPDuplicateValuesFoundException", StringComparison.InvariantCultureIgnoreCase) &&
                                applyingInformation.IgnoreDuplicateDataRowErrors)
                            {
                                scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_listitem_duplicate);
                                continue;
                            }
                            else
                            {
                                scope.LogError(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_listitem_failed___0_____1_, ex.Message, ex.StackTrace);
                                throw;
                            }
                        }
                    }
                }

                #endregion
            }

            return(parser);
        }
Beispiel #7
0
        private ListItem SetValues(string[] keyValues,
                                   ListItem listItem,
                                   ref T sEntity,
                                   bool toLocalTime)
        {
            foreach (var keyValue in keyValues)
            {
                try
                {
                    bool readOnly         = false;
                    var  key              = keyValue.Split('=')[0];
                    var  value            = WebUtility.UrlDecode(keyValue.Split('=')[1]);
                    var  property         = SpNameUtility.GetProperty(key, typeof(T)); //typeof(T).GetProperty(SpNameUtility.GetPropertyName(key, typeof(T)));
                    var  customAttributes = property.GetCustomAttributes(typeof(SpproFieldAttribute), true);

                    if (customAttributes.Count() > 0)
                    {
                        var attribute = (SpproFieldAttribute)customAttributes[0];
                        readOnly = attribute.ReadOnly;
                    }
                    if (!readOnly)
                    {
                        object finalValue = value;
                        var    targetType = IsNullableType(property.PropertyType) ? Nullable.GetUnderlyingType(property.PropertyType) : property.PropertyType;
                        if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
                        {
                            var format = value.Split('[', ']')[1];
                            value = value.Substring(0, value.IndexOf('['));
                            DateTime dateValue = new DateTime();;
                            if (DateTime.TryParseExact(value, format, null, System.Globalization.DateTimeStyles.None, out dateValue))
                            {
                                if (toLocalTime)
                                {
                                    dateValue = dateValue.ToLocalTime();
                                }
                                finalValue = dateValue;
                                property.SetValue(sEntity, Convert.ChangeType(dateValue, targetType));
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else if (property.PropertyType == typeof(GeoCoordinate))
                        {
                            var latitude  = Convert.ToDouble(value.Split(',')[0]);
                            var longitude = Convert.ToDouble(value.Split(',')[1]);
                            var geoValue  = new FieldGeolocationValue();
                            geoValue.Latitude  = latitude;
                            geoValue.Longitude = longitude;
                            finalValue         = geoValue;
                            var geoLocation = new GeoCoordinate(latitude, longitude);
                            property.SetValue(sEntity, Convert.ChangeType(geoLocation, targetType));
                        }
                        else if (property.PropertyType == typeof(Microsoft.SharePoint.Client.FieldLookupValue))
                        {
                            finalValue = new FieldLookupValue();
                            ((FieldLookupValue)finalValue).LookupId = Convert.ToInt32(value);
                            property.SetValue(sEntity, finalValue);
                        }
                        else if (property.PropertyType == typeof(Microsoft.SharePoint.Client.FieldUrlValue))
                        {
                            finalValue = new FieldUrlValue()
                            {
                                Description = value.Split(',')[0], Url = WebUtility.UrlDecode(value.Split(',')[1])
                            };
                            property.SetValue(sEntity, finalValue);
                        }
                        else if (property.PropertyType == typeof(Microsoft.SharePoint.Client.FieldUserValue))
                        {
                            int userId;
                            if (int.TryParse(value, out userId))
                            {
                                finalValue = new FieldUserValue()
                                {
                                    LookupId = userId
                                };
                                property.SetValue(sEntity, finalValue);
                            }
                        }
                        else if (property.PropertyType == typeof(string[]))
                        {
                            finalValue = value.Split(new string[] { "<<,>>" }, StringSplitOptions.RemoveEmptyEntries);
                            property.SetValue(sEntity, finalValue);
                        }
                        else
                        {
                            if (!(finalValue.ToString() == "" && TypeUtility.IsNumeric(targetType)))
                            {
                                if (finalValue.ToString() == "NaN")
                                {
                                    finalValue = 0;
                                }
                                property.SetValue(sEntity, Convert.ChangeType(finalValue, targetType));
                            }
                        }
                        //Get SharePoint Field Name
                        key           = SpNameUtility.GetSPFieldName(key, typeof(T));
                        listItem[key] = finalValue;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("error with " + keyValue, ex);
                }
            }

            return(listItem);
        }
        public override void ProvisionObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows);

            if (template.Lists.Any())
            {
                var rootWeb = (web.Context as ClientContext).Site.RootWeb;
                if (!web.IsPropertyAvailable("ServerRelativeUrl"))
                {
                    web.Context.Load(web, w => w.ServerRelativeUrl);
                    web.Context.ExecuteQueryRetry();
                }

                web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
                web.Context.ExecuteQueryRetry();
                var existingLists = web.Lists.AsEnumerable<List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
                var serverRelativeUrl = web.ServerRelativeUrl;

                #region DataRows

                foreach (var listInstance in template.Lists)
                {
                    if (listInstance.DataRows != null && listInstance.DataRows.Any())
                    {
                        // Retrieve the target list
                        var list = web.Lists.GetByTitle(listInstance.Title);
                        web.Context.Load(list);

                        // Retrieve the fields' types from the list
                        FieldCollection fields = list.Fields;
                        web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
                        web.Context.ExecuteQueryRetry();

                        foreach (var dataRow in listInstance.DataRows)
                        {
                            var listitemCI = new ListItemCreationInformation();
                            var listitem = list.AddItem(listitemCI);

                            foreach (var dataValue in dataRow.Values)
                            {
                                Field dataField = fields.FirstOrDefault(
                                    f => f.InternalName == dataValue.Key.ToParsedString());

                                if (dataField != null)
                                {
                                    String fieldValue = dataValue.Value.ToParsedString();

                                    switch (dataField.FieldTypeKind)
                                    {
                                        case FieldType.Geolocation:
                                            // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                            var geolocationArray = fieldValue.Split(',');
                                            if (geolocationArray.Length == 4)
                                            {
                                                var geolocationValue = new FieldGeolocationValue
                                                {
                                                    Altitude = Double.Parse(geolocationArray[0]),
                                                    Latitude = Double.Parse(geolocationArray[1]),
                                                    Longitude = Double.Parse(geolocationArray[2]),
                                                    Measure = Double.Parse(geolocationArray[3]),
                                                };
                                                listitem[dataValue.Key.ToParsedString()] = geolocationValue;
                                            }
                                            else
                                            {
                                                listitem[dataValue.Key.ToParsedString()] = fieldValue;
                                            }
                                            break;
                                        case FieldType.Lookup:
                                            // FieldLookupValue - Expected format: LookupID
                                            var lookupValue = new FieldLookupValue
                                            {
                                                LookupId = Int32.Parse(fieldValue),
                                            };
                                            listitem[dataValue.Key.ToParsedString()] = lookupValue;
                                            break;
                                        case FieldType.URL:
                                            // FieldUrlValue - Expected format: URL,Description
                                            var urlArray = fieldValue.Split(',');
                                            var linkValue = new FieldUrlValue();
                                            if (urlArray.Length == 2)
                                            {
                                                linkValue.Url = urlArray[0];
                                                linkValue.Description = urlArray[1];
                                            }
                                            else
                                            {
                                                linkValue.Url = urlArray[0];
                                                linkValue.Description = urlArray[0];
                                            }
                                            listitem[dataValue.Key.ToParsedString()] = linkValue;
                                            break;
                                        case FieldType.User:
                                            // FieldUserValue - Expected format: loginName
                                            var user = web.EnsureUser(fieldValue);
                                            web.Context.Load(user);
                                            web.Context.ExecuteQueryRetry();

                                            if (user != null)
                                            {
                                                var userValue = new FieldUserValue
                                                {
                                                    LookupId = user.Id,
                                                };
                                                listitem[dataValue.Key.ToParsedString()] = userValue;
                                            }
                                            else
                                            {
                                                listitem[dataValue.Key.ToParsedString()] = fieldValue;
                                            }
                                            break;
                                        default:
                                            listitem[dataValue.Key.ToParsedString()] = fieldValue;
                                            break;
                                    }
                                }
                                listitem.Update();
                            }
                            web.Context.ExecuteQueryRetry(); // TODO: Run in batches?
                        }
                    }
                }
                #endregion
            }
        }
 public override object GetFieldValueTyped(string value)
 {
     object itemValue = value;
     var geolocationArray = value.Split(',');
     if (geolocationArray.Length == 4)
     {
         var geolocationValue = new FieldGeolocationValue
         {
             Altitude = Double.Parse(geolocationArray[0]),
             Latitude = Double.Parse(geolocationArray[1]),
             Longitude = Double.Parse(geolocationArray[2]),
             Measure = Double.Parse(geolocationArray[3]),
         };
         itemValue = geolocationValue;
     }
     return itemValue;
 }
Beispiel #10
0
        public override void ProvisionObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows);

            if (template.Lists.Any())
            {
                var rootWeb = (web.Context as ClientContext).Site.RootWeb;
                if (!web.IsPropertyAvailable("ServerRelativeUrl"))
                {
                    web.Context.Load(web, w => w.ServerRelativeUrl);
                    web.Context.ExecuteQueryRetry();
                }

                web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
                web.Context.ExecuteQueryRetry();
                var existingLists     = web.Lists.AsEnumerable <List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
                var serverRelativeUrl = web.ServerRelativeUrl;

                #region DataRows

                foreach (var listInstance in template.Lists)
                {
                    if (listInstance.DataRows != null && listInstance.DataRows.Any())
                    {
                        // Retrieve the target list
                        var list = web.Lists.GetByTitle(listInstance.Title);
                        web.Context.Load(list);

                        // Retrieve the fields' types from the list
                        FieldCollection fields = list.Fields;
                        web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
                        web.Context.ExecuteQueryRetry();

                        foreach (var dataRow in listInstance.DataRows)
                        {
                            var listitemCI = new ListItemCreationInformation();
                            var listitem   = list.AddItem(listitemCI);

                            foreach (var dataValue in dataRow.Values)
                            {
                                Field dataField = fields.FirstOrDefault(
                                    f => f.InternalName == dataValue.Key.ToParsedString());

                                if (dataField != null)
                                {
                                    String fieldValue = dataValue.Value.ToParsedString();

                                    switch (dataField.FieldTypeKind)
                                    {
                                    case FieldType.Geolocation:
                                        // FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
                                        var geolocationArray = fieldValue.Split(',');
                                        if (geolocationArray.Length == 4)
                                        {
                                            var geolocationValue = new FieldGeolocationValue
                                            {
                                                Altitude  = Double.Parse(geolocationArray[0]),
                                                Latitude  = Double.Parse(geolocationArray[1]),
                                                Longitude = Double.Parse(geolocationArray[2]),
                                                Measure   = Double.Parse(geolocationArray[3]),
                                            };
                                            listitem[dataValue.Key.ToParsedString()] = geolocationValue;
                                        }
                                        else
                                        {
                                            listitem[dataValue.Key.ToParsedString()] = fieldValue;
                                        }
                                        break;

                                    case FieldType.Lookup:
                                        // FieldLookupValue - Expected format: LookupID
                                        var lookupValue = new FieldLookupValue
                                        {
                                            LookupId = Int32.Parse(fieldValue),
                                        };
                                        listitem[dataValue.Key.ToParsedString()] = lookupValue;
                                        break;

                                    case FieldType.URL:
                                        // FieldUrlValue - Expected format: URL,Description
                                        var urlArray  = fieldValue.Split(',');
                                        var linkValue = new FieldUrlValue();
                                        if (urlArray.Length == 2)
                                        {
                                            linkValue.Url         = urlArray[0];
                                            linkValue.Description = urlArray[1];
                                        }
                                        else
                                        {
                                            linkValue.Url         = urlArray[0];
                                            linkValue.Description = urlArray[0];
                                        }
                                        listitem[dataValue.Key.ToParsedString()] = linkValue;
                                        break;

                                    case FieldType.User:
                                        // FieldUserValue - Expected format: loginName
                                        var user = web.EnsureUser(fieldValue);
                                        web.Context.Load(user);
                                        web.Context.ExecuteQueryRetry();

                                        if (user != null)
                                        {
                                            var userValue = new FieldUserValue
                                            {
                                                LookupId = user.Id,
                                            };
                                            listitem[dataValue.Key.ToParsedString()] = userValue;
                                        }
                                        else
                                        {
                                            listitem[dataValue.Key.ToParsedString()] = fieldValue;
                                        }
                                        break;

                                    default:
                                        listitem[dataValue.Key.ToParsedString()] = fieldValue;
                                        break;
                                    }
                                }
                                listitem.Update();
                            }
                            web.Context.ExecuteQueryRetry(); // TODO: Run in batches?
                        }
                    }
                }
                #endregion
            }
        }