Exemplo n.º 1
0
        protected override void ExecuteCmdlet()
        {
            var serverRelativeUrl = string.Empty;

            var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);

            if (!Url.ToLower().StartsWith(webUrl.ToLower()))
            {
                serverRelativeUrl = UrlUtility.Combine(webUrl, Url);
            }
            else
            {
                serverRelativeUrl = Url;
            }

            File file;

            file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));

            ClientContext.Load(file, f => f.Exists, f => f.Versions.IncludeWithDefaultProperties(i => i.CreatedBy));
            ClientContext.ExecuteQueryRetry();

            if (file.Exists)
            {
                var versions = file.Versions;

                switch (ParameterSetName)
                {
                case ParameterSetName_ALL:
                {
                    if (Force || ShouldContinue("Remove all versions?", Resources.Confirm))
                    {
                        versions.DeleteAll();
                        ClientContext.ExecuteQueryRetry();
                    }
                    break;
                }

                case ParameterSetName_BYID:
                {
                    if (Force || ShouldContinue("Remove a version?", Resources.Confirm))
                    {
                        if (!string.IsNullOrEmpty(Identity.Label))
                        {
                            if (Recycle.IsPresent)
                            {
                                versions.RecycleByLabel(Identity.Label);
                            }
                            else
                            {
                                versions.DeleteByLabel(Identity.Label);
                            }
                            ClientContext.ExecuteQueryRetry();
                        }
                        else if (Identity.Id != -1)
                        {
                            if (Recycle.IsPresent)
                            {
                                versions.RecycleByID(Identity.Id);
                            }
                            else
                            {
                                versions.DeleteByID(Identity.Id);
                            }
                            ClientContext.ExecuteQueryRetry();
                        }
                    }
                    break;
                }
                }
            }
            else
            {
                throw new PSArgumentException("File not found", nameof(Url));
            }
        }
Exemplo n.º 2
0
        protected override void ExecuteCmdlet()
        {
            var serverRelativeUrl = string.Empty;

            if (string.IsNullOrEmpty(Path))
            {
                Path = SessionState.Path.CurrentFileSystemLocation.Path;
            }
            else
            {
                if (!System.IO.Path.IsPathRooted(Path))
                {
                    Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
                }
            }

            var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);

            if (!Url.ToLower().StartsWith(webUrl.ToLower()))
            {
                serverRelativeUrl = UrlUtility.Combine(webUrl, Url);
            }
            else
            {
                serverRelativeUrl = Url;
            }

            File file;

            switch (ParameterSetName)
            {
            case URLTOPATH:

                SaveFileToLocal(CurrentWeb, serverRelativeUrl, Path, Filename, (fileToSave) =>
                {
                    if (!Force)
                    {
                        WriteWarning($"File '{fileToSave}' exists already. use the -Force parameter to overwrite the file.");
                    }
                    return(Force);
                }).Wait();
                break;

            case URLASFILEOBJECT:
                file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
                try
                {
                    ClientContext.Load(file, f => f.Author, f => f.Length, f => f.ModifiedBy, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title);
                    ClientContext.ExecuteQueryRetry();
                }
                catch (ServerException)
                {
                    // Assume the cause of the exception is that a principal cannot be found and try again without:
                    // Fallback in case the creator or person having last modified the file no longer exists in the environment such that the file can still be downloaded
                    ClientContext.Load(file, f => f.Length, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title);
                    ClientContext.ExecuteQueryRetry();
                }
                WriteObject(file);
                break;

            case URLASLISTITEM:
                file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));

                ClientContext.Load(file, f => f.Exists, f => f.ListItemAllFields);

                ClientContext.ExecuteQueryRetry();
                if (file.Exists)
                {
                    WriteObject(file.ListItemAllFields);
                }
                else
                {
                    if (ThrowExceptionIfFileNotFound)
                    {
                        throw new PSArgumentException($"No file found with the provided Url {serverRelativeUrl}", "Url");
                    }
                }
                break;

            case URLASSTRING:
                WriteObject(CurrentWeb.GetFileAsString(serverRelativeUrl));
                break;
            }
        }
Exemplo n.º 3
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;
                }
            }

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

            string targetContentTypeId = 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)
            {
                CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
                var list = CurrentWeb.GetListByUrl(folder.ServerRelativeUrl.Substring(CurrentWeb.ServerRelativeUrl.TrimEnd('/').Length + 1));
                if (list is null)
                {
                    throw new PSArgumentException("The folder specified does not have a corresponding list", nameof(Folder));
                }
                targetContentTypeId = ContentType?.GetIdOrThrow(nameof(ContentType), list);
            }

            // Check if the file exists
            if (Checkout)
            {
                try
                {
                    var existingFile = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(fileUrl));

                    existingFile.EnsureProperty(f => f.Exists);
                    if (existingFile.Exists)
                    {
                        CurrentWeb.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);
            }

            bool updateRequired = false;
            var  item           = file.ListItemAllFields;

            if (Values != null)
            {
                ListItemHelper.SetFieldValues(item, Values, this);
                updateRequired = true;
            }

            if (ContentType != null)
            {
                item["ContentTypeId"] = targetContentTypeId;
                updateRequired        = true;
            }

            if (updateRequired)
            {
                item.UpdateOverwriteVersion();
            }
            if (Checkout)
            {
                CurrentWeb.CheckInFile(fileUrl, CheckinType.MajorCheckIn, CheckInComment);
            }

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

            if (Approve)
            {
                CurrentWeb.ApproveFile(fileUrl, ApproveComment);
            }

            ClientContext.Load(file);
            try
            {
                ClientContext.ExecuteQueryRetry();
            }
            catch (ServerException)
            {
                // Can happen when uploading a file to a location not residing inside a document library, such as to the _cts folder. Switch to fallback option so that the upload doesn't result in an error.
                ClientContext.Load(file, f => f.Length, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title);
                ClientContext.ExecuteQueryRetry();
            }
            WriteObject(file);
        }