Skip to content

Commit

Permalink
SetListItem: Throw when item can't be found and warn when content typ…
Browse files Browse the repository at this point in the history
…e can't be found
  • Loading branch information
fowl2 committed Sep 29, 2022
1 parent d502498 commit 8a2eba1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 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
105 changes: 52 additions & 53 deletions src/Commands/Lists/SetListItem.cs
Original file line number Diff line number Diff line change
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 8a2eba1

Please sign in to comment.