Пример #1
0
        protected override void ExecuteCmdlet()
        {
            if (ParameterSetName == ParameterSet_ASFILE)
            {
                if (!System.IO.Path.IsPathRooted(Path))
                {
                    Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
                }
                if (string.IsNullOrEmpty(NewFileName))
                {
                    FileName = System.IO.Path.GetFileName(Path);
                }
                else
                {
                    FileName = NewFileName;
                }
            }

            SelectedWeb.EnsureProperty(w => w.ServerRelativeUrl);

            var folder  = SelectedWeb.EnsureFolder(SelectedWeb.RootFolder, Folder);
            var fileUrl = UrlUtility.Combine(folder.ServerRelativeUrl, FileName);

            ContentType targetContentType = null;

            //Check to see if the Content Type exists.. If it doesn't we are going to throw an exception and block this transaction right here.
            if (ContentType != null)
            {
                try
                {
                    var list = SelectedWeb.GetListByUrl(folder.ServerRelativeUrl);


                    if (!string.IsNullOrEmpty(ContentType.Id))
                    {
                        targetContentType = list.GetContentTypeById(ContentType.Id);
                    }
                    else if (!string.IsNullOrEmpty(ContentType.Name))
                    {
                        targetContentType = list.GetContentTypeByName(ContentType.Name);
                    }
                    else if (ContentType.ContentType != null)
                    {
                        targetContentType = ContentType.ContentType;
                    }
                    if (targetContentType == null)
                    {
                        ThrowTerminatingError(new ErrorRecord(new ArgumentException($"Content Type Argument: {ContentType} does not exist in the list: {list.Title}"), "CONTENTTYPEDOESNOTEXIST", ErrorCategory.InvalidArgument, this));
                    }
                }
                catch
                {
                    ThrowTerminatingError(new ErrorRecord(new ArgumentException($"The Folder specified ({folder.ServerRelativeUrl}) does not have a corresponding List, the -ContentType parameter is not valid."), "RELATIVEPATHNOTINLIBRARY", ErrorCategory.InvalidArgument, this));
                }
            }

            // Check if the file exists
            if (Checkout)
            {
                try
                {
#if ONPREMISES
                    var existingFile = SelectedWeb.GetFileByServerRelativeUrl(fileUrl);
#else
                    var existingFile = SelectedWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(fileUrl));
#endif
                    existingFile.EnsureProperty(f => f.Exists);
                    if (existingFile.Exists)
                    {
                        SelectedWeb.CheckOutFile(fileUrl);
                    }
                }
                catch
                { // Swallow exception, file does not exist
                }
            }
            Microsoft.SharePoint.Client.File file;
            if (ParameterSetName == ParameterSet_ASFILE)
            {
                file = folder.UploadFile(FileName, Path, true);
            }
            else
            {
                file = folder.UploadFile(FileName, Stream, true);
            }

            if (Values != null)
            {
                var item = file.ListItemAllFields;

                ListItemHelper.UpdateListItem(item, Values, ListItemUpdateType.UpdateOverwriteVersion,
                                              (warning) =>
                {
                    WriteWarning(warning);
                },
                                              (terminatingErrorMessage, terminatingErrorCode) =>
                {
                    ThrowTerminatingError(new ErrorRecord(new Exception(terminatingErrorMessage), terminatingErrorCode, ErrorCategory.InvalidData, this));
                });
            }
            if (ContentType != null)
            {
                var item = file.ListItemAllFields;
                item["ContentTypeId"] = targetContentType.Id.StringValue;
#if !ONPREMISES
                item.UpdateOverwriteVersion();
#else
                item.Update();
#endif
                ClientContext.ExecuteQueryRetry();
            }

            if (Checkout)
            {
                SelectedWeb.CheckInFile(fileUrl, CheckinType.MajorCheckIn, CheckInComment);
            }


            if (Publish)
            {
                SelectedWeb.PublishFile(fileUrl, PublishComment);
            }

            if (Approve)
            {
                SelectedWeb.ApproveFile(fileUrl, ApproveComment);
            }
            ClientContext.Load(file);
            ClientContext.ExecuteQueryRetry();
            WriteObject(file);
        }
Пример #2
0
        protected override void ExecuteCmdlet()
        {
            List list = null;

            if (List != null)
            {
                list = List.GetList(SelectedWeb);
            }
            if (list != null)
            {
                ListItemCreationInformation liCI = new ListItemCreationInformation();
                if (Folder != null)
                {
                    // Create the folder if it doesn't exist
                    var rootFolder   = list.EnsureProperty(l => l.RootFolder);
                    var targetFolder =
                        SelectedWeb.EnsureFolder(rootFolder, Folder);

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

                if (ContentType != null)
                {
                    ContentType ct = null;
                    if (ContentType.ContentType == null)
                    {
                        if (ContentType.Id != null)
                        {
                            ct = SelectedWeb.GetContentTypeById(ContentType.Id, true);
                        }
                        else if (ContentType.Name != null)
                        {
                            ct = SelectedWeb.GetContentTypeByName(ContentType.Name, true);
                        }
                    }
                    else
                    {
                        ct = ContentType.ContentType;
                    }
                    if (ct != null)
                    {
                        ct.EnsureProperty(w => w.StringId);

                        item["ContentTypeId"] = ct.StringId;
                        item.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                }

                if (Values != null)
                {
                    item = ListItemHelper.UpdateListItem(item, Values, false,
                                                         (warning) =>
                    {
                        WriteWarning(warning);
                    },
                                                         (terminatingErrorMessage, terminatingErrorCode) =>
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception(terminatingErrorMessage), terminatingErrorCode, ErrorCategory.InvalidData, this));
                    });
                }

                item.Update();
                ClientContext.Load(item);
                ClientContext.ExecuteQueryRetry();
                WriteObject(item);
            }
        }
Пример #3
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);
                }
            }
        }
Пример #4
0
        protected override void ExecuteCmdlet()
        {
            List list = null;

            if (List != null)
            {
                list = List.GetList(SelectedWeb);
            }
            if (list != null)
            {
                var item = Identity.GetListItem(list);

                if (ContentType != null)
                {
                    ContentType ct = null;
                    if (ContentType.ContentType == null)
                    {
                        if (ContentType.Id != null)
                        {
                            ct = SelectedWeb.GetContentTypeById(ContentType.Id, true);
                        }
                        else if (ContentType.Name != null)
                        {
                            ct = SelectedWeb.GetContentTypeByName(ContentType.Name, true);
                        }
                    }
                    else
                    {
                        ct = ContentType.ContentType;
                    }
                    if (ct != null)
                    {
                        ct.EnsureProperty(w => w.StringId);
                        item["ContentTypeId"] = ct.StringId;
                        if (SystemUpdate.IsPresent)
                        {
                            item.SystemUpdate();
                        }
                        else
                        {
                            item.Update();
                        }
                        ClientContext.ExecuteQueryRetry();
                    }
                }
                if (Values != null)
                {
                    var updateType = ListItemUpdateType.Update;
                    if (SystemUpdate.IsPresent)
                    {
                        updateType = ListItemUpdateType.SystemUpdate;
                    }
                    item = ListItemHelper.UpdateListItem(item, Values, updateType, (warning) =>
                    {
                        WriteWarning(warning);
                    },
                                                         (terminatingErrorMessage, terminatingErrorCode) =>
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception(terminatingErrorMessage), terminatingErrorCode, ErrorCategory.InvalidData, 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)
                    {
                        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);
                    }
                }
                WriteObject(item);
            }
        }
Пример #5
0
        protected override void ExecuteCmdlet()
        {
            List list = null;

            if (List != null)
            {
                list = List.GetList(SelectedWeb);
            }
            if (list != null)
            {
                var item = Identity.GetListItem(list);

                if (ContentType != null)
                {
                    ContentType ct = null;
                    if (ContentType.ContentType == null)
                    {
                        if (ContentType.Id != null)
                        {
                            ct = SelectedWeb.GetContentTypeById(ContentType.Id, true);
                        }
                        else if (ContentType.Name != null)
                        {
                            ct = SelectedWeb.GetContentTypeByName(ContentType.Name, true);
                        }
                    }
                    else
                    {
                        ct = ContentType.ContentType;
                    }
                    if (ct != null)
                    {
                        ct.EnsureProperty(w => w.StringId);

                        item["ContentTypeId"] = ct.StringId;
                        item.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                }
                if (Values != null)
                {
#if !ONPREMISES
                    item = ListItemHelper.UpdateListItem(item, Values, SystemUpdate, (warning) =>
                    {
                        WriteWarning(warning);
                    },
                                                         (terminatingErrorMessage, terminatingErrorCode) =>
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception(terminatingErrorMessage), terminatingErrorCode, ErrorCategory.InvalidData, this));
                    }
                                                         );
#else
                    item = ListItemHelper.UpdateListItem(item, Values, false, (warning) =>
                    {
                        WriteWarning(warning);
                    },
                                                         (terminatingErrorMessage, terminatingErrorCode) =>
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception(terminatingErrorMessage), terminatingErrorCode, ErrorCategory.InvalidData, this));
                    }
                                                         );
#endif
                }
                WriteObject(item);
            }
        }
Пример #6
0
        protected override void ExecuteCmdlet()
        {
            List list = null;

            if (List != null)
            {
                list = List.GetList(SelectedWeb);
            }
            if (list != null)
            {
                ListItemCreationInformation liCI = new ListItemCreationInformation();
                if (Folder != null)
                {
                    // Create the folder if it doesn't exist
                    var rootFolder   = list.EnsureProperty(l => l.RootFolder);
                    var targetFolder =
                        SelectedWeb.EnsureFolder(rootFolder, Folder);

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

                if (ContentType != null)
                {
                    ContentType ct = null;
                    if (ContentType.ContentType == null)
                    {
                        if (ContentType.Id != null)
                        {
                            ct = SelectedWeb.GetContentTypeById(ContentType.Id, true);
                        }
                        else if (ContentType.Name != null)
                        {
                            ct = SelectedWeb.GetContentTypeByName(ContentType.Name, true);
                        }
                    }
                    else
                    {
                        ct = ContentType.ContentType;
                    }
                    if (ct != null)
                    {
                        ct.EnsureProperty(w => w.StringId);

                        item["ContentTypeId"] = ct.StringId;
                        item.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                }

                if (Values != null)
                {
                    item = ListItemHelper.UpdateListItem(item, Values, ListItemUpdateType.Update,
                                                         (warning) =>
                    {
                        WriteWarning(warning);
                    },
                                                         (terminatingErrorMessage, terminatingErrorCode) =>
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception(terminatingErrorMessage), terminatingErrorCode, ErrorCategory.InvalidData, 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);
                    }
                }

                item.Update();
                ClientContext.Load(item);
                ClientContext.ExecuteQueryRetry();
                WriteObject(item);
            }
        }
Пример #7
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);
                }
            }
        }