Exemplo n.º 1
0
        public override bool Write(string fileName, byte[] data)
        {
            try
            {
                SharepointClientContext.Load(SharepointList.RootFolder);
                SharepointClientContext.ExecuteQuery();

                string fileUrl = String.Format("{0}/{1}", SharepointList.RootFolder.ServerRelativeUrl, fileName);
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(_clientContext, fileUrl, new MemoryStream(data, false), true);
                _clientContext.ExecuteQuery(); //Uploaded .. but still checked out...


                //Load the FieldCollection from the list...
                SP.FieldCollection fileFields = SharepointList.Fields;
                _clientContext.Load(fileFields);
                _clientContext.ExecuteQuery();

                SP.File     uploadedFile    = _web.GetFileByServerRelativeUrl(fileUrl);
                SP.ListItem newFileListItem = uploadedFile.ListItemAllFields;
                newFileListItem.Update();
                _clientContext.ExecuteQuery();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public void addColumns(string listTitle)
        {
            Web web = mClientContext.Web;

            SP.List projList = mClientContext.Web.Lists.GetByTitle(listTitle);
            // Adding the Custom field to the List. Here the OOB SPFieldText has been selected as the “FieldType”
            SP.FieldCollection collFields = projList.Fields;
            collFields.AddFieldAsXml("<Field DisplayName='Estimated Start Time' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Estimated Start Date' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Estimated End Time' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Estimated End Date' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Actual Start Time' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Actual End Time' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Actual Start Date' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Actual End Date' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Milestone Number' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Milestone Comment' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Time Spent' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Resources' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Current Status' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            collFields.AddFieldAsXml("<Field DisplayName='Current Status Reason' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            //test field
            //collFields.AddFieldAsXml("<Field DisplayName='test' Type='Choice' />", true, AddFieldOptions.DefaultValue);
            //SP.Field oField = collFields.GetByTitle("MyNewField");
            Console.WriteLine("Executing end of adding columns");
            projList.Update();
            mClientContext.ExecuteQuery();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Tokenizes calculated fieldXml to use tokens for field references
        /// </summary>
        /// <param name="fields">the field collection that the field is contained within</param>
        /// <param name="field">the field to tokenize</param>
        /// <param name="fieldXml">the xml to tokenize</param>
        /// <returns></returns>
        internal static string TokenizeFieldFormula(Microsoft.SharePoint.Client.FieldCollection fields, FieldCalculated field, string fieldXml)
        {
            var schemaElement = XElement.Parse(fieldXml);

            var formulaElement = schemaElement.Descendants("Formula").FirstOrDefault();

            if (formulaElement != null)
            {
                field.EnsureProperty(f => f.Formula);

                var formulastring = field.Formula;

                if (formulastring != null)
                {
                    var fieldRefs = schemaElement.Descendants("FieldRef");
                    foreach (var fieldRef in fieldRefs)
                    {
                        var fieldInternalName = fieldRef.Attribute("Name").Value;
                        var referencedField   = fields.GetFieldByInternalName(fieldInternalName);
                        formulastring = formulastring.Replace($"[{referencedField.Title}]", $"[{{fieldtitle:{fieldInternalName}}}]");
                    }
                    var fieldRefParent = schemaElement.Descendants("FieldRefs");
                    fieldRefParent.Remove();

                    formulaElement.Value = formulastring;
                }
            }
            return(schemaElement.ToString());
        }
Exemplo n.º 4
0
        public static void LoadSiteColumns(TreeNode parentNode, SPClient.FieldCollection fields, MainBrowser form, LoadType loadType)
        {
            try
            {
                SPClient.ClientContext ctx = GetClientContext(parentNode);
                ctx.Load(fields);
                ctx.ExecuteQuery();

                int total   = fields.Count;
                int current = 0;

                foreach (SPClient.Field field in fields)
                {
                    TreeNode node = parentNode.Nodes.Add(string.Format("{0} ({1})", field.Title, field.Group));
                    node.ImageKey         = Constants.IMAGE_SITE_COLUMN;
                    node.SelectedImageKey = Constants.IMAGE_SITE_COLUMN;
                    node.Tag = field;
                    node.ContextMenuStrip = form.mnContextItem;

                    if (field.Hidden)
                    {
                        node.ForeColor = Color.Gray;
                    }

                    // Add group node
                    TreeNode groupNode = parentNode.Nodes.OfType <TreeNode>().SingleOrDefault(n => n.Text.Equals(field.Group));
                    if (groupNode == null)
                    {
                        groupNode                  = new TreeNode(field.Group);
                        groupNode.ImageKey         = Constants.IMAGE_SITE_COLUMN_GROUP;
                        groupNode.SelectedImageKey = Constants.IMAGE_SITE_COLUMN_GROUP;
                        groupNode.Tag              = LoadType.GeneralGroup;

                        parentNode.Nodes.Add(groupNode);
                    }
                    groupNode.Nodes.Add((TreeNode)node.Clone());

                    // Update progress
                    current++;
                    ItemLoaded(null, new ItemLoadedEventArgs()
                    {
                        TotalItem = total, CurrentItem = current
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, form.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                AddLoadingNode(parentNode, loadType);
            }
        }
Exemplo n.º 5
0
        protected override void ExecuteCmdlet()
        {
            if (!System.IO.Path.IsPathRooted(Path))
            {
                Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
            }

            var template = ReadSiteTemplate
                           .LoadSiteTemplateFromFile(Path,
                                                     TemplateProviderExtensions, (e) =>
            {
                WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
            });

            if (template == null)
            {
                throw new ApplicationException("Invalid template file!");
            }
            //We will remove a list if it's found so we can get the list

            ListInstance listInstance = template.Lists.Find(l => l.Title == List.Title);

            if (listInstance == null)
            {
                throw new ApplicationException("List does not exist in the template file!");
            }

            List spList = List.GetList(SelectedWeb);

            ClientContext.Load(spList, l => l.RootFolder, l => l.HasUniqueRoleAssignments);
            ClientContext.ExecuteQueryRetry();

            if (TokenizeUrls.IsPresent)
            {
                ClientContext.Load(ClientContext.Web, w => w.Url, w => w.ServerRelativeUrl, w => w.Id);
                ClientContext.Load(ClientContext.Site, s => s.Url, s => s.ServerRelativeUrl, s => s.Id);
                ClientContext.Load(ClientContext.Web.Lists, lists => lists.Include(l => l.Title, l => l.RootFolder.ServerRelativeUrl));
            }

            CamlQuery query = new CamlQuery();

            var viewFieldsStringBuilder = new StringBuilder();

            if (Fields != null)
            {
                viewFieldsStringBuilder.Append("<ViewFields>");
                foreach (var field in Fields)
                {
                    viewFieldsStringBuilder.AppendFormat("<FieldRef Name='{0}'/>", field);
                }
                viewFieldsStringBuilder.Append("</ViewFields>");
            }

            query.ViewXml = string.Format("<View>{0}{1}</View>", Query, viewFieldsStringBuilder);
            var listItems = spList.GetItems(query);

            ClientContext.Load(listItems, lI => lI.Include(l => l.HasUniqueRoleAssignments, l => l.ContentType.StringId));
            ClientContext.ExecuteQueryRetry();

            Microsoft.SharePoint.Client.FieldCollection fieldCollection = spList.Fields;
            ClientContext.Load(fieldCollection, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind, f => f.ReadOnlyField));
            ClientContext.ExecuteQueryRetry();

            var rows = new DataRowCollection(template);

            foreach (var listItem in listItems)
            {
                //Make sure we don't pull Folders.. Of course this won't work
                if (listItem.ServerObjectIsNull == false)
                {
                    ClientContext.Load(listItem);
                    ClientContext.ExecuteQueryRetry();
                    if (!(listItem.FileSystemObjectType == FileSystemObjectType.Folder))
                    {
                        DataRow row = new DataRow();
                        if (IncludeSecurity && listItem.HasUniqueRoleAssignments)
                        {
                            row.Security.ClearSubscopes      = true;
                            row.Security.CopyRoleAssignments = false;

                            var roleAssignments = listItem.RoleAssignments;
                            ClientContext.Load(roleAssignments);
                            ClientContext.ExecuteQueryRetry();

                            ClientContext.Load(roleAssignments, r => r.Include(a => a.Member.LoginName, a => a.Member, a => a.RoleDefinitionBindings));
                            ClientContext.ExecuteQueryRetry();

                            foreach (var roleAssignment in roleAssignments)
                            {
                                var principalName = roleAssignment.Member.LoginName;
                                var roleBindings  = roleAssignment.RoleDefinitionBindings;
                                foreach (var roleBinding in roleBindings)
                                {
                                    row.Security.RoleAssignments.Add(new PnP.Framework.Provisioning.Model.RoleAssignment()
                                    {
                                        Principal = principalName, RoleDefinition = roleBinding.Name
                                    });
                                }
                            }
                        }
                        if (Fields != null)
                        {
                            foreach (var fieldName in Fields)
                            {
                                Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName);

                                if (dataField != null)
                                {
                                    var defaultFieldValue = GetFieldValueAsText(ClientContext.Web, listItem, dataField);
                                    if (TokenizeUrls.IsPresent)
                                    {
                                        defaultFieldValue = Tokenize(defaultFieldValue, ClientContext.Web, ClientContext.Site);
                                    }

                                    row.Values.Add(fieldName, defaultFieldValue);
                                }
                            }
                        }
                        else
                        {
                            //All fields are added except readonly fields and unsupported field type
                            var fieldsToExport = fieldCollection.AsEnumerable()
                                                 .Where(f => !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind));
                            foreach (var field in fieldsToExport)
                            {
                                var fldKey = (from f in listItem.FieldValues.Keys where f == field.InternalName select f).FirstOrDefault();
                                if (!string.IsNullOrEmpty(fldKey))
                                {
                                    var fieldValue = GetFieldValueAsText(ClientContext.Web, listItem, field);
                                    if (TokenizeUrls.IsPresent)
                                    {
                                        fieldValue = Tokenize(fieldValue, ClientContext.Web, ClientContext.Site);
                                    }
                                    row.Values.Add(field.InternalName, fieldValue);
                                }
                            }
                        }

                        rows.Add(row);
                    }
                }
            }
            template.Lists.Remove(listInstance);
            listInstance.DataRows.AddRange(rows);
            template.Lists.Add(listInstance);

            var outFileName = System.IO.Path.GetFileName(Path);
            var outPath     = new FileInfo(Path).DirectoryName;

            var fileSystemConnector = new FileSystemConnector(outPath, "");
            var formatter           = XMLPnPSchemaFormatter.LatestFormatter;
            var extension           = new FileInfo(Path).Extension.ToLowerInvariant();

            if (extension == ".pnp")
            {
                XMLTemplateProvider provider = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(Path, fileSystemConnector));
                var templateFileName         = outFileName.Substring(0, outFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";

                provider.SaveAs(template, templateFileName, formatter, TemplateProviderExtensions);
            }
            else
            {
                XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(Path, "");
                provider.SaveAs(template, Path, formatter, TemplateProviderExtensions);
            }
        }
Exemplo n.º 6
0
        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())
                    {
                        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;

                                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)
                                {
                                    if (listitem == null)
                                    {
                                        var listitemCI = new ListItemCreationInformation();
                                        listitem = list.AddItem(listitemCI);
                                    }

                                    ListItemUtilities.UpdateListItem(listitem, parser, dataRow.Values, ListItemUtilities.ListItemUpdateType.UpdateOverwriteVersion);

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

                #endregion DataRows
            }

            return(parser);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                web.EnsureProperties(w => w.ServerRelativeUrl);

                // determine pages library
                string pagesLibrary = "SitePages";

                List <string> preCreatedPages = new List <string>();

                var currentPageIndex = 0;
                // pre create the needed pages so we can fill the needed tokens which might be used later on when we put web parts on those pages
                foreach (var clientSidePage in template.ClientSidePages)
                {
                    string pageName = $"{System.IO.Path.GetFileNameWithoutExtension(parser.ParseString(clientSidePage.PageName))}.aspx";
                    string url      = $"{pagesLibrary}/{pageName}";

                    // Write page level status messages, needed in case many pages are provisioned
                    currentPageIndex++;
                    WriteMessage($"ClientSidePage|Create {pageName}|{currentPageIndex}|{template.ClientSidePages.Count}", ProvisioningMessageType.Progress);

                    url = UrlUtility.Combine(web.ServerRelativeUrl, url);

                    var exists = true;
                    try
                    {
                        var file = web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(url));
                        web.Context.Load(file, f => f.UniqueId, f => f.ServerRelativePath);
                        web.Context.ExecuteQueryRetry();

                        // Fill token
                        parser.AddToken(new PageUniqueIdToken(web, file.ServerRelativePath.DecodedUrl.Substring(web.ServerRelativeUrl.Length).TrimStart("/".ToCharArray()), file.UniqueId));
                        parser.AddToken(new PageUniqueIdEncodedToken(web, file.ServerRelativePath.DecodedUrl.Substring(web.ServerRelativeUrl.Length).TrimStart("/".ToCharArray()), file.UniqueId));
                    }
                    catch (ServerException ex)
                    {
                        if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException")
                        {
                            exists = false;
                        }
                    }

                    if (!exists)
                    {
                        // Pre-create the page
                        Pages.ClientSidePage page = web.AddClientSidePage(pageName);

                        // Set page layout now, because once it's set, it can't be changed.
                        if (!string.IsNullOrEmpty(clientSidePage.Layout))
                        {
                            if (clientSidePage.Layout.Equals("Article", StringComparison.InvariantCultureIgnoreCase))
                            {
                                page.LayoutType = Pages.ClientSidePageLayoutType.Article;
                            }
                            else if (clientSidePage.Layout.Equals("Home", StringComparison.InvariantCultureIgnoreCase))
                            {
                                page.LayoutType = Pages.ClientSidePageLayoutType.Home;
                            }
                            else if (clientSidePage.Layout.Equals("SingleWebPartAppPage", StringComparison.InvariantCultureIgnoreCase))
                            {
                                page.LayoutType = Pages.ClientSidePageLayoutType.SingleWebPartAppPage;
                            }
                        }

                        page.Save(pageName);

                        var file = web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(url));
                        web.Context.Load(file, f => f.UniqueId, f => f.ServerRelativePath);
                        web.Context.ExecuteQueryRetry();

                        // Fill token
                        parser.AddToken(new PageUniqueIdToken(web, file.ServerRelativePath.DecodedUrl.Substring(web.ServerRelativeUrl.Length).TrimStart("/".ToCharArray()), file.UniqueId));
                        parser.AddToken(new PageUniqueIdEncodedToken(web, file.ServerRelativePath.DecodedUrl.Substring(web.ServerRelativeUrl.Length).TrimStart("/".ToCharArray()), file.UniqueId));

                        // Track that we pre-added this page
                        preCreatedPages.Add(url);
                    }
                }

                currentPageIndex = 0;
                // Iterate over the pages and create/update them
                foreach (var clientSidePage in template.ClientSidePages)
                {
                    string pageName = $"{System.IO.Path.GetFileNameWithoutExtension(parser.ParseString(clientSidePage.PageName))}.aspx";
                    string url      = $"{pagesLibrary}/{pageName}";
                    // Write page level status messages, needed in case many pages are provisioned
                    currentPageIndex++;
                    WriteMessage($"ClientSidePage|{pageName}|{currentPageIndex}|{template.ClientSidePages.Count}", ProvisioningMessageType.Progress);

                    url = UrlUtility.Combine(web.ServerRelativeUrl, url);

                    var exists = true;
                    try
                    {
                        var file = web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(url));
                        web.Context.Load(file);
                        web.Context.ExecuteQueryRetry();
                    }
                    catch (ServerException ex)
                    {
                        if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException")
                        {
                            exists = false;
                        }
                    }

                    Pages.ClientSidePage page = null;
                    if (exists)
                    {
                        if (clientSidePage.Overwrite || preCreatedPages.Contains(url))
                        {
                            // Get the existing page
                            page = web.LoadClientSidePage(pageName);
                            // Clear the page
                            page.ClearPage();
                        }
                        else
                        {
                            scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ClientSidePages_NoOverWrite, pageName);
                            continue;
                        }
                    }
                    else
                    {
                        // Create new client side page
                        page = web.AddClientSidePage(pageName);
                    }

                    // Set page title
                    string newTitle = parser.ParseString(clientSidePage.Title);
                    if (page.PageTitle != newTitle)
                    {
                        page.PageTitle = newTitle;
                    }

                    // Page Header
                    if (clientSidePage.Header != null)
                    {
                        switch (clientSidePage.Header.Type)
                        {
                        case ClientSidePageHeaderType.None:
                        {
                            page.RemovePageHeader();
                            break;
                        }

                        case ClientSidePageHeaderType.Default:
                        {
                            page.SetDefaultPageHeader();
                            break;
                        }

                        case ClientSidePageHeaderType.Custom:
                        {
                            var serverRelativeImageUrl = parser.ParseString(clientSidePage.Header.ServerRelativeImageUrl);
                            if (clientSidePage.Header.TranslateX.HasValue && clientSidePage.Header.TranslateY.HasValue)
                            {
                                page.SetCustomPageHeader(serverRelativeImageUrl, clientSidePage.Header.TranslateX.Value, clientSidePage.Header.TranslateY.Value);
                            }
                            else
                            {
                                page.SetCustomPageHeader(serverRelativeImageUrl);
                            }
                            break;
                        }
                        }
                    }

                    // Set page layout
                    if (!string.IsNullOrEmpty(clientSidePage.Layout))
                    {
                        if (clientSidePage.Layout.Equals("Article", StringComparison.InvariantCultureIgnoreCase))
                        {
                            page.LayoutType = Pages.ClientSidePageLayoutType.Article;
                        }
                        else if (clientSidePage.Layout.Equals("Home", StringComparison.InvariantCultureIgnoreCase))
                        {
                            page.LayoutType = Pages.ClientSidePageLayoutType.Home;
                        }
                        else if (clientSidePage.Layout.Equals("SingleWebPartAppPage", StringComparison.InvariantCultureIgnoreCase))
                        {
                            page.LayoutType = Pages.ClientSidePageLayoutType.SingleWebPartAppPage;
                        }
                    }

                    // Load existing available controls
                    var componentsToAdd = page.AvailableClientSideComponents().ToList();

                    // if no section specified then add a default single column section
                    if (!clientSidePage.Sections.Any())
                    {
                        clientSidePage.Sections.Add(new CanvasSection()
                        {
                            Type = CanvasSectionType.OneColumn, Order = 10
                        });
                    }

                    int sectionCount = -1;
                    // Apply the "layout" and content
                    foreach (var section in clientSidePage.Sections)
                    {
                        sectionCount++;
                        switch (section.Type)
                        {
                        case CanvasSectionType.OneColumn:
                            page.AddSection(Pages.CanvasSectionTemplate.OneColumn, section.Order);
                            break;

                        case CanvasSectionType.OneColumnFullWidth:
                            page.AddSection(Pages.CanvasSectionTemplate.OneColumnFullWidth, section.Order);
                            break;

                        case CanvasSectionType.TwoColumn:
                            page.AddSection(Pages.CanvasSectionTemplate.TwoColumn, section.Order);
                            break;

                        case CanvasSectionType.ThreeColumn:
                            page.AddSection(Pages.CanvasSectionTemplate.ThreeColumn, section.Order);
                            break;

                        case CanvasSectionType.TwoColumnLeft:
                            page.AddSection(Pages.CanvasSectionTemplate.TwoColumnLeft, section.Order);
                            break;

                        case CanvasSectionType.TwoColumnRight:
                            page.AddSection(Pages.CanvasSectionTemplate.TwoColumnRight, section.Order);
                            break;

                        default:
                            page.AddSection(Pages.CanvasSectionTemplate.OneColumn, section.Order);
                            break;
                        }

                        // Add controls to the section
                        if (section.Controls.Any())
                        {
                            // Safety measure: reset column order to 1 for columns marked with 0 or lower
                            foreach (var control in section.Controls.Where(p => p.Column <= 0).ToList())
                            {
                                control.Column = 1;
                            }

                            foreach (CanvasControl control in section.Controls)
                            {
                                Pages.ClientSideComponent baseControl = null;

                                // Is it a text control?
                                if (control.Type == WebPartType.Text)
                                {
                                    Pages.ClientSideText textControl = new Pages.ClientSideText();
                                    if (control.ControlProperties.Any())
                                    {
                                        var textProperty = control.ControlProperties.First();
                                        textControl.Text = parser.ParseString(textProperty.Value);
                                    }
                                    else
                                    {
                                        if (!string.IsNullOrEmpty(control.JsonControlData))
                                        {
                                            var json = JsonConvert.DeserializeObject <Dictionary <string, string> >(control.JsonControlData);

                                            if (json.Count > 0)
                                            {
                                                textControl.Text = parser.ParseString(json.First().Value);
                                            }
                                        }
                                    }
                                    // Reduce column number by 1 due 0 start indexing
                                    page.AddControl(textControl, page.Sections[sectionCount].Columns[control.Column - 1], control.Order);
                                }
                                // It is a web part
                                else
                                {
                                    // apply token parsing on the web part properties
                                    control.JsonControlData = parser.ParseString(control.JsonControlData);

                                    // perform processing of web part properties (e.g. include listid property based list title property)
                                    var webPartPostProcessor = CanvasControlPostProcessorFactory.Resolve(control);
                                    webPartPostProcessor.Process(control, page);

                                    // Is a custom developed client side web part (3rd party)
                                    if (control.Type == WebPartType.Custom)
                                    {
                                        if (!string.IsNullOrEmpty(control.CustomWebPartName))
                                        {
                                            baseControl = componentsToAdd.FirstOrDefault(p => p.Name.Equals(control.CustomWebPartName, StringComparison.InvariantCultureIgnoreCase));
                                        }
                                        else if (control.ControlId != Guid.Empty)
                                        {
                                            baseControl = componentsToAdd.FirstOrDefault(p => p.Id.Equals($"{{{control.ControlId.ToString()}}}", StringComparison.CurrentCultureIgnoreCase));

                                            if (baseControl == null)
                                            {
                                                baseControl = componentsToAdd.FirstOrDefault(p => p.Id.Equals(control.ControlId.ToString(), StringComparison.InvariantCultureIgnoreCase));
                                            }
                                        }
                                    }
                                    // Is an OOB client side web part (1st party)
                                    else
                                    {
                                        string webPartName = "";
                                        switch (control.Type)
                                        {
                                        case WebPartType.Image:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.Image);
                                            break;

                                        case WebPartType.BingMap:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.BingMap);
                                            break;

                                        case WebPartType.ContentEmbed:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.ContentEmbed);
                                            break;

                                        case WebPartType.ContentRollup:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.ContentRollup);
                                            break;

                                        case WebPartType.DocumentEmbed:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.DocumentEmbed);
                                            break;

                                        case WebPartType.Events:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.Events);
                                            break;

                                        case WebPartType.GroupCalendar:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.GroupCalendar);
                                            break;

                                        case WebPartType.Hero:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.Hero);
                                            break;

                                        case WebPartType.ImageGallery:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.ImageGallery);
                                            break;

                                        case WebPartType.LinkPreview:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.LinkPreview);
                                            break;

                                        case WebPartType.List:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.List);
                                            break;

                                        case WebPartType.NewsFeed:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.NewsFeed);
                                            break;

                                        case WebPartType.NewsReel:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.NewsReel);
                                            break;

                                        case WebPartType.PageTitle:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.PageTitle);
                                            break;

                                        case WebPartType.People:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.People);
                                            break;

                                        case WebPartType.PowerBIReportEmbed:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.PowerBIReportEmbed);
                                            break;

                                        case WebPartType.QuickChart:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.QuickChart);
                                            break;

                                        case WebPartType.QuickLinks:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.QuickLinks);
                                            break;

                                        case WebPartType.SiteActivity:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.SiteActivity);
                                            break;

                                        case WebPartType.VideoEmbed:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.VideoEmbed);
                                            break;

                                        case WebPartType.YammerEmbed:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.YammerEmbed);
                                            break;

                                        case WebPartType.CustomMessageRegion:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.CustomMessageRegion);
                                            break;

                                        case WebPartType.Divider:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.Divider);
                                            break;

                                        case WebPartType.MicrosoftForms:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.MicrosoftForms);
                                            break;

                                        case WebPartType.Spacer:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.Spacer);
                                            break;

                                        case WebPartType.ClientWebPart:
                                            webPartName = Pages.ClientSidePage.ClientSideWebPartEnumToName(Pages.DefaultClientSideWebParts.ClientWebPart);
                                            break;
                                        }

                                        baseControl = componentsToAdd.FirstOrDefault(p => p.Name.Equals(webPartName, StringComparison.InvariantCultureIgnoreCase));
                                    }

                                    if (baseControl != null)
                                    {
                                        Pages.ClientSideWebPart myWebPart = new Pages.ClientSideWebPart(baseControl)
                                        {
                                            Order = control.Order
                                        };

                                        if (!String.IsNullOrEmpty(control.JsonControlData))
                                        {
                                            var json = JsonConvert.DeserializeObject <JObject>(control.JsonControlData);
                                            if (json["instanceId"] != null && json["instanceId"].Type != JTokenType.Null)
                                            {
                                                if (Guid.TryParse(json["instanceId"].Value <string>(), out Guid instanceId))
                                                {
                                                    myWebPart.instanceId = instanceId;
                                                }
                                            }
                                        }

                                        // Reduce column number by 1 due 0 start indexing
                                        page.AddControl(myWebPart, page.Sections[sectionCount].Columns[control.Column - 1], control.Order);

                                        // set properties using json string
                                        if (!String.IsNullOrEmpty(control.JsonControlData))
                                        {
                                            myWebPart.PropertiesJson = control.JsonControlData;
                                        }

                                        // set using property collection
                                        if (control.ControlProperties.Any())
                                        {
                                            // grab the "default" properties so we can deduct their types, needed to correctly apply the set properties
                                            var    controlManifest   = JObject.Parse(baseControl.Manifest);
                                            JToken controlProperties = null;
                                            if (controlManifest != null)
                                            {
                                                controlProperties = controlManifest.SelectToken("preconfiguredEntries[0].properties");
                                            }

                                            foreach (var property in control.ControlProperties)
                                            {
                                                Type propertyType = typeof(string);

                                                if (controlProperties != null)
                                                {
                                                    var defaultProperty = controlProperties.SelectToken(property.Key, false);
                                                    if (defaultProperty != null)
                                                    {
                                                        propertyType = Type.GetType($"System.{defaultProperty.Type.ToString()}");

                                                        if (propertyType == null)
                                                        {
                                                            if (defaultProperty.Type.ToString().Equals("integer", StringComparison.InvariantCultureIgnoreCase))
                                                            {
                                                                propertyType = typeof(int);
                                                            }
                                                        }
                                                    }
                                                }

                                                myWebPart.Properties[property.Key] = JToken.FromObject(Convert.ChangeType(parser.ParseString(property.Value), propertyType));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ClientSidePages_BaseControlNotFound, control.ControlId, control.CustomWebPartName);
                                    }
                                }
                            }
                        }
                    }

                    // Persist the page
                    page.Save(pageName);

                    if (clientSidePage.FieldValues != null && clientSidePage.FieldValues.Any())
                    {
                        List <FieldUpdateValue> fieldValues = clientSidePage.FieldValues.Select(s => new FieldUpdateValue(parser.ParseString(s.Key), parser.ParseString(s.Value))).ToList();
                        Microsoft.SharePoint.Client.FieldCollection fields = page.PageListItem.ParentList.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();
                        ListItemUtilities.UpdateListItem(web, page.PageListItem, fields, fieldValues);
                    }

                    if (page.LayoutType != Pages.ClientSidePageLayoutType.SingleWebPartAppPage)
                    {
                        // Set commenting, ignore on pages of the type Home
                        if (page.LayoutType != Pages.ClientSidePageLayoutType.Home)
                        {
                            // Make it a news page if requested
                            if (clientSidePage.PromoteAsNewsArticle)
                            {
                                page.PromoteAsNewsArticle();
                            }
                        }

                        if (clientSidePage.EnableComments)
                        {
                            page.EnableComments();
                        }
                        else
                        {
                            page.DisableComments();
                        }
                    }

                    // Publish page
                    if (clientSidePage.Publish)
                    {
                        page.Publish();
                    }

                    // Set any security on the page
                    if (clientSidePage.Security != null && clientSidePage.Security.RoleAssignments.Count != 0)
                    {
                        web.Context.Load(page.PageListItem);
                        web.Context.ExecuteQueryRetry();
                        page.PageListItem.SetSecurity(parser, clientSidePage.Security);
                    }
                }
            }

            WriteMessage("Done processing Client Side Pages", ProvisioningMessageType.Completed);
            return(parser);
        }
Exemplo n.º 9
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);
        }
        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);
        }
Exemplo n.º 11
0
        private void BindList(SP.FieldCollection collField)
        {
            // Load in the Fields
            clientContext.Load(collField);
            clientContext.ExecuteQuery();

            int i = 0;

            // Create a Row Number column
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

            col.Name       = "rowNumber";
            col.HeaderText = "Row Number";
            col.ValueType  = typeof(int);
            dgv_List.Columns.Add(col);

            // Update label text
            lbl_Summary.Text = "Adding columns to Data Grid View control";
            lbl_Summary.Refresh();

            // Loop through the fields in the list
            foreach (SP.Field oField in oList.Fields)
            {
                // We will not add computed fields
                bool add = true;

                // We will not show lookup fields
                bool show = true;

                // Determine whether to add/show field
                switch (oField.TypeAsString)
                {
                case "Computed":
                    add = false;
                    break;

                case "Lookup":
                    show = false;
                    break;
                }

                // Check the we are not adding computed fields as these are not included in the oListItem.FieldValues[] array
                if (add)
                {
                    // Increment row counter
                    i++;

                    // Create a Text Box column, set name and header
                    col            = new DataGridViewTextBoxColumn();
                    col.Name       = oField.InternalName;
                    col.HeaderText = oField.Title;
                    dgv_List.Columns.Add(col);
                    col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    col.Visible = show;

                    // Refresh for visual effect
                    if (i <= 20)
                    {
                        dgv_List.Refresh();
                    }
                }
            }

            // Update label text
            lbl_Summary.Text = "Added " + i.ToString() + " columns...";
            lbl_Summary.Refresh();

            // Create a new Caml Query
            CamlQuery camlQuery = new CamlQuery();

            // Set the XML
            camlQuery.ViewXml = "<View><Query>{0}</Query></View>";

            // Define a collection to store the list items in
            ListItemCollection collListItem = oList.GetItems(camlQuery);

            // Load in the items
            clientContext.Load(collListItem);

            // Attempt to retreive the List Items
            try
            {
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            // Determine the row limit
            int numRowsMax  = (int)nud_Row_Limit.Value;
            int numRowsList = collListItem.Count;
            int numRows     = numRowsMax;

            // Return all rows if no limit set
            if (numRows == 0)
            {
                numRows = numRowsList;
            }

            // Reset row counter
            i = 0;

            // Declare variable for reporting column lookup errors
            string colError = "";

            // Loop through each item in the collection
            foreach (ListItem oListItem in collListItem)
            {
                // Add a row to the Data Grid View control
                dgv_List.Rows.Add();

                // Reset column counter
                int j = 0;

                // Add the "row number" field we created manually
                dgv_List[j, i].Value = i + 1;

                // Loop through fields in the list
                foreach (SP.Field oField in oList.Fields)
                {
                    // Exclude computed fields
                    if (oField.TypeAsString != "Computed")
                    {
                        // Increment column counter
                        j++;

                        // Store the list/field value
                        try
                        {
                            var value = oListItem.FieldValues[oField.InternalName];

                            // Check for null
                            if (value != null)
                            {
                                /*
                                 * // Try and lookup object-based fields
                                 * switch (value.ToString())
                                 * {
                                 *  case "Microsoft.SharePoint.Client.FieldUserValue":
                                 *
                                 *      SP.FieldUserValue uField = (SP.FieldUserValue)value;
                                 *      value = uField.LookupValue.ToString();
                                 *
                                 *      break;
                                 *
                                 *  case "Microsoft.SharePoint.Client.FieldLookupValue[]":
                                 *
                                 *      value = null;
                                 *
                                 *      break;
                                 * }
                                 */

                                value = ChangeValue(value);
                            }

                            // Enter the value in the cell
                            dgv_List[j, i].Value = value;
                        }
                        catch (Exception ex)
                        {
                            // Report as lookup error
                            if (i == 0)
                            {
                                colError += oField.InternalName + "\r\n";
                            }

                            j--;
                        }
                    }
                }

                // Increment the row counter
                i++;

                // Update label text
                lbl_Summary.Text = "Returned " + i.ToString("#,##0") + " out of " + numRowsList.ToString("#,##0") + " record(s)";
                lbl_Summary.Refresh();

                // Jump out if we have reached the limit
                if (i >= numRows)
                {
                    break;
                }
            }

            // Report column lookup errors
            if (colError != "")
            {
                colError = "The following columns could not be found in the FieldValues array:\r\n\r\n" + colError;

                MessageBox.Show(colError, "Column Not Found");
            }

            // Finally, if any rows got added, auto-size the columns
            if (dgv_List.Rows.Count > 0 && dgv_List.Rows.Count <= 500)
            {
                string text = lbl_Summary.Text;
                lbl_Summary.Text = "Auto-sizing columns...";
                lbl_Summary.Refresh();

                foreach (DataGridViewColumn c in dgv_List.Columns)
                {
                    c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                    int width = c.Width;

                    if (width >= 300)
                    {
                        width = 300;
                    }

                    c.Width = width;

                    c.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                }

                lbl_Summary.Text = text;
                lbl_Summary.Refresh();

                cmd_Versions.Enabled = true;
            }
        }
Exemplo n.º 12
0
        private void cmd_Versions_Click(object sender, EventArgs e)
        {
            if (clientContext == null)
            {
                return;
            }

            frm_Test_List testForm = new frm_Test_List();

            // Disable / hide irrelevant controls
            testForm.cmd_Open_List.Visible = false;
            testForm.cmd_Versions.Visible  = false;
            testForm.txt_Guid.Visible      = false;
            testForm.txt_Url.Visible       = false;
            testForm.lbl_Guid.Visible      = false;
            testForm.lbl_Url.Visible       = false;
            testForm.nud_Row_Limit.Value   = 0;
            testForm.nud_Row_Limit.Enabled = false;

            testForm.Show();

            int col = dgv_List.Columns["ID"].Index;
            int row = dgv_List.CurrentRow.Index;
            int id  = (int)dgv_List[col, row].Value;

            // Find the specified item
            SP.ListItem oListItem = oList.GetItemById(id);

            // Load the Item
            clientContext.Load(oListItem);

            // Load the Versions
            clientContext.Load(oListItem.Versions);

            // Do this bit
            clientContext.ExecuteQuery();

            // Loop through each Version
            foreach (SP.ListItemVersion versionItem in oListItem.Versions)
            {
                // Retrieve the fields
                SP.FieldCollection collField = versionItem.Fields;

                clientContext.Load(collField);
                clientContext.ExecuteQuery();

                int i = 0;

                // Loop through fields
                foreach (SP.Field oField in collField)
                {
                    string fieldName  = oField.Title;
                    string fieldValue = "null";

                    try
                    {
                        if (oListItem[oField.InternalName] != null)
                        {
                            fieldValue = versionItem[oField.InternalName].ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        fieldValue = "Error";
                    }
                    finally
                    {
                        i++;
                    }
                }
            }
        }
Exemplo n.º 13
0
        static void CreateXmlItemsInSharePoint()
        {
            spClient.ClientContext spCtx = LoginCsom();

            spClient.List myList = spCtx.Web.Lists.GetByTitle(SpListName);
            spClient.ListItemCollection allItems = myList.GetItems(spClient.CamlQuery.CreateAllItemsQuery());
            spClient.FieldCollection    myFields = myList.Fields;
            spCtx.Load(myFields);
            spCtx.Load(allItems);
            spCtx.ExecuteQuery();

            Dictionary <string, string> fieldValues = new Dictionary <string, string>();

            foreach (Microsoft.SharePoint.Client.ListItem oneItem in allItems)
            {
                MemoryStream myStream = new MemoryStream();

                XmlWriterSettings mySettings = new XmlWriterSettings
                {
                    Indent             = true,
                    IndentChars        = ("    "),
                    CloseOutput        = true,
                    OmitXmlDeclaration = true
                };

                using (XmlWriter myWriter = XmlWriter.Create(myStream, mySettings))
                {
                    myWriter.WriteStartDocument(true);
                    myWriter.WriteStartElement("Item");

                    foreach (Microsoft.SharePoint.Client.Field oneField in myFields)
                    {
                        if (oneField.Hidden == false)
                        {
                            try
                            {
                                fieldValues.Add(oneField.Title, oneItem[oneField.Title].ToString());
                            }
                            catch
                            {
                                // In case there is more than one field with the same name
                            }
                        }
                    }

                    foreach (string oneKey in fieldValues.Keys)
                    {
                        myWriter.WriteStartElement(oneKey.Replace(" ", "_"));
                        myWriter.WriteString(fieldValues[oneKey]);
                        myWriter.WriteEndElement();
                    }

                    fieldValues.Clear();
                    myWriter.WriteEndElement();
                    myWriter.WriteEndDocument();
                    myWriter.Flush();

                    try
                    {
                        spClient.AttachmentCreationInformation attInfo = new spClient.AttachmentCreationInformation();
                        attInfo.FileName      = oneItem["Title"] + ".xml";
                        attInfo.ContentStream = new MemoryStream(myStream.ToArray());
                        oneItem.AttachmentFiles.Add(attInfo);
                        spCtx.ExecuteQuery();
                    }
                    catch
                    {
                        // In case the attachment already exists
                    }
                }
            }
        }