/// <summary>
        /// Copies the file versioned.
        /// </summary>
        /// <param name="i">The i.</param>
        /// <param name="folderTo">The folder to.</param>
        /// <param name="srcFile">The version.</param>
        /// <param name="destUrl">The dest URL.</param>
        /// <returns></returns>
        private SPFile CopyFileVersioned(int i,
                                         SPFolder folderTo,
                                         SPFileVersion srcFile,
                                         string destUrl)
        {
            Debug.WriteLine("* Versioned ");
            Debug.WriteLine("i " + i);
            Debug.WriteLine("folderTo " + folderTo.Url);
            Debug.WriteLine("srcFile " + srcFile.VersionLabel);
            Debug.WriteLine("destUrl " + destUrl);

            Debug.WriteLine("EnsureCheckOut");

            EnsureCheckOut(folderTo, i, srcFile.File, destUrl);

            Debug.WriteLine("Add File to new folder");

            SPFile newFile = folderTo.Files.Add(destUrl,
                                                srcFile.OpenBinary(),
                                                true,
                                                srcFile.CheckInComment,
                                                false);

            EnsureCheckIn(newFile, srcFile.CheckInComment);
            CopyMetadata(srcFile.File, newFile, false);
            newFile.Item["Created"]  = srcFile.Created;
            newFile.Item["Modified"] = srcFile.Created;
            newFile.Item["Author"]   = srcFile.CreatedBy;
            newFile.Item.UpdateOverwriteVersion();


            Debug.WriteLine("* COPIED " + newFile.Name + " ");

            return(newFile);
        }
    static void Main(string[] args)
    {
        using (SPSite site = new SPSite("https://sharepoint.domain.com"))
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.GetList($"{web.ServerRelativeUrl.TrimEnd('/')}/DocumentLibrary");
                SPListItemCollection items = list.GetItems(new SPQuery());

                foreach (SPListItem item in items)
                {
                    object checkedOutTo = item[SPBuiltInFieldId.CheckoutUser];
                    if (checkedOutTo == null)
                    {
                        // Latest version
                        Console.WriteLine($"{item.ID} | {item.Versions[0].VersionLabel}");

                        // Here are bytes of the file itself
                        byte[] bytes = item.File.OpenBinary();
                    }
                    else
                    {
                        // Find latest published version
                        SPFileVersion version = item.File.Versions
                                                .Cast <SPFileVersion>()
                                                .OrderByDescending(v => v.ID)
                                                .Where(v => v.Level == SPFileLevel.Published)
                                                .FirstOrDefault();

                        if (version != null)
                        {
                            Console.WriteLine($"{item.ID} | {version.VersionLabel} | {checkedOutTo}");

                            // Here are bytes of the file itself
                            byte[] bytes = version.OpenBinary();
                        }
                        else
                        {
                            Console.WriteLine($"{item.ID} | No published version | {checkedOutTo}");
                        }
                    }
                }
            }
    }
Beispiel #3
0
        public Base64EncodedByteArrayInstance OpenBinary()
        {
            var result = new Base64EncodedByteArrayInstance(Engine.Object.InstancePrototype, m_fileVersion.OpenBinary())
            {
                FileName = m_fileVersion.File.Name,
                MimeType = StringHelper.GetMimeTypeFromFileName(m_fileVersion.File.Name)
            };

            return(result);
        }