Skip to content

Commit

Permalink
SetListItem: fix -SystemUpdate:$false
Browse files Browse the repository at this point in the history
  • Loading branch information
fowl2 committed Sep 29, 2022
1 parent 0dc3baf commit dd94a6c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 54 deletions.
9 changes: 9 additions & 0 deletions src/Commands/Base/PipeBinds/ContentTypePipeBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ internal PnPCore.IContentType GetContentTypeOrWarn(Cmdlet cmdlet, PnPCore.IList
return ct;
}

internal PnP.Core.Model.SharePoint.IContentType GetContentTypeOrWarn(Cmdlet cmdlet, PnPBatch batch, PnP.Core.Model.SharePoint.IList list)
{
var ct = GetContentType(batch, list);
if (ct is null)
cmdlet.WriteWarning(NotFoundMessage(list));

return ct;
}

private string NotFoundMessage(bool searchInSiteHierarchy)
=> $"Content type '{this}' not found in site{(searchInSiteHierarchy ? " hierarchy" : "")}.";

Expand Down
107 changes: 53 additions & 54 deletions src/Commands/Lists/SetListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class SetListItem : PnPWebCmdlet
protected override void ExecuteCmdlet()
{
#pragma warning disable CS0618
if (ParameterSpecified(nameof(SystemUpdate)))
if (SystemUpdate)
{
UpdateType = ListItemUpdateType.SystemUpdate;
}
Expand Down Expand Up @@ -159,76 +159,75 @@ private void SetListItemSingle()
list = Identity.Item.ParentList;
}

if (list != null)
{
var item = Identity.GetListItem(list);
bool itemUpdated = false;
var item = Identity.GetListItem(list)
?? throw new PSArgumentException($"Provided -Identity is not valid.", nameof(Identity)); ;

if (ClearLabel)
{
item.SetComplianceTag(string.Empty, false, false, false, false, false);
ClientContext.ExecuteQueryRetry();
itemUpdated = true;
}
bool itemUpdated = false;

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

var tag = tags.Where(t => t.TagName == Label).FirstOrDefault();
if (!string.IsNullOrEmpty(Label))
{
var tags = Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetAvailableTagsForSite(ClientContext, ClientContext.Url);
ClientContext.ExecuteQueryRetry();

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);
}
itemUpdated = true;
}
var tag = tags.Where(t => t.TagName == Label).FirstOrDefault();

if (ContentType != null)
if (tag is null)
{
WriteWarning("Can not find compliance tag with value: " + Label);
}
else
{
ContentType ct = ContentType.GetContentType(list);
if (ct != null)
try
{
item["ContentTypeId"] = ct.EnsureProperty(w => w.StringId); ;
ListItemHelper.UpdateListItem(item, UpdateType);
item.SetComplianceTag(tag.TagName, tag.BlockDelete, tag.BlockEdit, tag.IsEventTag, tag.SuperLock, false);
ClientContext.ExecuteQueryRetry();
}
itemUpdated = true;
catch (System.Exception error)
{
WriteWarning(error.Message.ToString());
}
}
itemUpdated = true;
}

if (Values?.Count > 0)
if (ContentType != null)
{
ContentType ct = ContentType.GetContentTypeOrWarn(this, list);
if (ct != null)
{
ListItemHelper.SetFieldValues(item, Values, this);
item["ContentTypeId"] = ct.EnsureProperty(w => w.StringId); ;
ListItemHelper.UpdateListItem(item, UpdateType);
itemUpdated = true;
ClientContext.ExecuteQueryRetry();
}
itemUpdated = true;
}

if (!itemUpdated && !Force)
{
WriteWarning("No values provided. Pass -Force to update anyway.");
}
else
{
ListItemHelper.UpdateListItem(item, UpdateType);
}
if (Values?.Count > 0)
{
ListItemHelper.SetFieldValues(item, Values, this);
ListItemHelper.UpdateListItem(item, UpdateType);
itemUpdated = true;
}

ClientContext.ExecuteQueryRetry();
ClientContext.Load(item);
WriteObject(item);
if (!itemUpdated && !Force)
{
WriteWarning("No values provided. Pass -Force to update anyway.");
}
else
{
ListItemHelper.UpdateListItem(item, UpdateType);
}

ClientContext.ExecuteQueryRetry();
ClientContext.Load(item);
WriteObject(item);
}
}
}

0 comments on commit dd94a6c

Please sign in to comment.