Esempio n. 1
0
        protected override void ExecuteCmdlet()
        {
#pragma warning disable CS0618
            if (ParameterSpecified(nameof(SystemUpdate)))
            {
                UpdateType = ListItemUpdateType.SystemUpdate;
            }
#pragma warning restore CS0618
            if (ParameterSpecified(nameof(Batch)))
            {
                var list = List.GetList(Batch);
                if (list != null)
                {
                    var item = Identity.GetListItem(list);
                    if (item == null)
                    {
                        throw new PSArgumentException($"Cannot find item with Identity {Identity}", nameof(Identity));
                    }
                    var values = ListItemHelper.GetFieldValues(list, item, Values, ClientContext, Batch);
                    if (values == null)
                    {
                        values = new Dictionary <string, object>();
                    }
                    if (ContentType != null)
                    {
                        var ct = ContentType.GetContentType(Batch, list);
                        if (ct != null)
                        {
                            values.Add("ContentTypeId", ct.StringId);
                        }
                    }
                    foreach (var value in values)
                    {
                        item[value.Key] = values[value.Key];
                    }
                    switch (UpdateType)
                    {
                    case ListItemUpdateType.SystemUpdate:
                    {
                        item.SystemUpdateBatch(Batch.Batch);
                        break;
                    }

                    case ListItemUpdateType.UpdateOverwriteVersion:
                    {
                        item.UpdateOverwriteVersionBatch(Batch.Batch);
                        break;
                    }

                    case ListItemUpdateType.Update:
                    {
                        item.UpdateBatch(Batch.Batch);
                        break;
                    }
                    }
                }
            }
            else
            {
                if (Identity == null || (Identity.Item == null && Identity.Id == 0))
                {
                    throw new PSArgumentException($"No -Identity has been provided specifying the item to update", nameof(Identity));
                }

                List list;
                if (List != null)
                {
                    list = List.GetList(CurrentWeb);
                }
                else
                {
                    if (Identity.Item == null)
                    {
                        throw new PSArgumentException($"No -List has been provided specifying the list to update the item in", nameof(Identity));
                    }

                    list = Identity.Item.ParentList;
                }

                if (list != null)
                {
                    var item = Identity.GetListItem(list);

                    if (ParameterSpecified(nameof(ClearLabel)))
                    {
                        item.SetComplianceTag(string.Empty, false, false, false, false, false);
                        ClientContext.ExecuteQueryRetry();
                    }
                    if (!string.IsNullOrEmpty(Label))
                    {
                        var tags = Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetAvailableTagsForSite(ClientContext, ClientContext.Url);
                        ClientContext.ExecuteQueryRetry();

                        var tag = tags.Where(t => t.TagName == Label).FirstOrDefault();

                        if (tag != null)
                        {
                            try
                            {
                                item.SetComplianceTag(tag.TagName, tag.BlockDelete, tag.BlockEdit, tag.IsEventTag, tag.SuperLock, false);
                                ClientContext.ExecuteQueryRetry();
                            }
                            catch (System.Exception error)
                            {
                                WriteWarning(error.Message.ToString());
                            }
                        }
                        else
                        {
                            WriteWarning("Can not find compliance tag with value: " + Label);
                        }
                    }

                    if (ContentType != null)
                    {
                        ContentType ct = ContentType.GetContentType(list);
                        if (ct != null)
                        {
                            item["ContentTypeId"] = ct.EnsureProperty(w => w.StringId);;
                            ListItemHelper.UpdateListItem(item, UpdateType);
                            ClientContext.ExecuteQueryRetry();
                        }
                    }
                    if (Values != null)
                    {
                        ListItemHelper.SetFieldValues(item, Values, this);
                        ListItemHelper.UpdateListItem(item, UpdateType);
                    }
                    ClientContext.ExecuteQueryRetry();
                    ClientContext.Load(item);
                    WriteObject(item);
                }
            }
        }
Esempio n. 2
0
        protected override void ExecuteCmdlet()
        {
            if (ParameterSpecified(nameof(Batch)))
            {
                var list = List.GetList(Batch);
                list.EnsureProperties(l => l.Id, l => l.Fields.LoadProperties(f => f.Id, f => f.Title, f => f.InternalName, f => f.TypeAsString));

                var values = ListItemHelper.GetFieldValues(list, null, Values, ClientContext);
                if (ContentType != null)
                {
                    var contentType = ContentType.GetContentType(Batch, list);
                    values.Add("ContentTypeId", contentType.StringId);
                }
                list.Items.AddBatch(Batch.Batch, values, Folder);
            }
            else
            {
                List list = List.GetList(CurrentWeb);
                ListItemCreationInformation liCI = new ListItemCreationInformation();
                if (Folder != null)
                {
                    // Create the folder if it doesn't exist
                    var rootFolder   = list.EnsureProperty(l => l.RootFolder);
                    var targetFolder =
                        CurrentWeb.EnsureFolder(rootFolder, Folder);

                    liCI.FolderUrl = targetFolder.ServerRelativeUrl;
                }
                var item = list.AddItem(liCI);

                bool systemUpdate = false;
                if (ContentType != null)
                {
                    var ct = ContentType.GetContentType(list);

                    if (ct != null)
                    {
                        item["ContentTypeId"] = ct.EnsureProperty(w => w.StringId);
                        item.Update();
                        systemUpdate = true;
                        ClientContext.ExecuteQueryRetry();
                    }
                }

                if (Values?.Count > 0)
                {
                    ListItemHelper.SetFieldValues(item, Values, this);
                }

                if (!String.IsNullOrEmpty(Label))
                {
                    IList <Microsoft.SharePoint.Client.CompliancePolicy.ComplianceTag> tags = Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetAvailableTagsForSite(ClientContext, ClientContext.Url);
                    ClientContext.ExecuteQueryRetry();

                    var tag = tags.Where(t => t.TagName == Label).FirstOrDefault();

                    if (tag != null)
                    {
                        item.SetComplianceTag(tag.TagName, tag.BlockDelete, tag.BlockEdit, tag.IsEventTag, tag.SuperLock);
                    }
                    else
                    {
                        WriteWarning("Can not find compliance tag with value: " + Label);
                    }
                }

                if (systemUpdate)
                {
                    item.SystemUpdate();
                }
                else
                {
                    item.Update();
                }
                ClientContext.Load(item);
                ClientContext.ExecuteQueryRetry();
                WriteObject(item);
            }
        }
Esempio n. 3
0
        protected override void ExecuteCmdlet()
        {
            if (ParameterSpecified(nameof(Batch)))
            {
                var list = List.GetList(Batch);
                if (list != null)
                {
                    var item = Identity.GetListItem(list);
                    if (item == null)
                    {
                        throw new PSArgumentException($"Cannot find item with Identity {Identity}");
                    }
                    var values = ListItemHelper.GetFieldValues(list, Values, ClientContext);
                    if (values == null)
                    {
                        values = new Dictionary <string, object>();
                    }
                    if (ContentType != null)
                    {
                        var ct = ContentType.GetContentType(Batch, list);
                        if (ct != null)
                        {
                            values.Add("ContentTypeId", ct.StringId);
                        }
                    }
                    foreach (var value in values)
                    {
                        item[value.Key] = values[value.Key];
                    }
                    if (SystemUpdate)
                    {
                        item.SystemUpdateBatch(Batch.Batch);
                    }
                    else
                    {
                        item.UpdateBatch(Batch.Batch);
                    }
                }
            }
            else
            {
                List list = List.GetList(CurrentWeb);

                if (list != null)
                {
                    var item = Identity.GetListItem(list);

                    bool updateRequired = false;

                    if (ContentType != null)
                    {
                        ContentType ct = ContentType.GetContentType(list);

                        if (ct != null)
                        {
                            item["ContentTypeId"] = ct.EnsureProperty(w => w.StringId);;
                            updateRequired        = true;
                        }
                    }
                    if (Values != null)
                    {
                        ListItemHelper.SetFieldValues(item, Values, this);
                        updateRequired = true;
                    }

                    if (ParameterSpecified(nameof(ClearLabel)))
                    {
                        item.SetComplianceTag(string.Empty, false, false, false, false);
                        ClientContext.ExecuteQueryRetry();
                    }
                    if (!string.IsNullOrEmpty(Label))
                    {
                        var tags = Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetAvailableTagsForSite(ClientContext, ClientContext.Url);
                        ClientContext.ExecuteQueryRetry();

                        var tag = tags.Where(t => t.TagName == Label).FirstOrDefault();

                        if (tag != null)
                        {
                            try
                            {
                                item.SetComplianceTag(tag.TagName, tag.BlockDelete, tag.BlockEdit, tag.IsEventTag, tag.SuperLock);
                                ClientContext.ExecuteQueryRetry();
                            }
                            catch (System.Exception error)
                            {
                                WriteWarning(error.Message.ToString());
                            }
                        }
                        else
                        {
                            WriteWarning("Can not find compliance tag with value: " + Label);
                        }
                    }

                    if (updateRequired)
                    {
                        var updateType = ListItemUpdateType.Update;
                        if (SystemUpdate.IsPresent)
                        {
                            updateType = ListItemUpdateType.SystemUpdate;
                        }
                        ListItemHelper.UpdateListItem(item, updateType);
                    }
                    ClientContext.ExecuteQueryRetry();
                    ClientContext.Load(item);
                    WriteObject(item);
                }
            }
        }