0

In the code below, I am copying a file from root web library to a sub-web's library.

The issue is that Modified by, Created by, and Modified are not being copied/set properly. Perhaps there are other properties which are not correct, but these are the ones I discovered so far.

PS: Why the versions keep incrementing if I copy the file again without deleting it first? I thought the flag overwrite set to true should do the job.

Any idea why these properties are not being copied and how can I solve it?

This is my code:

public static void CopyFile()
        {
            string sourceSiteUrl = "[SourceUrl]/folderA";
            string destSiteUrl = "[destinationUrl]/folderB";
            string srcUrl = "[SourceUrl]/folderA/File1.xlsx";
            using (SPSite siteSrc = new SPSite(sourceSiteUrl))
            using (SPSite siteDst = new SPSite(destSiteUrl))
            using (SPWeb webSrc = siteSrc.OpenWeb())
            using (SPWeb webDst = siteDst.OpenWeb())
            {
                SPFile fileSource = webSrc.GetFile(srcUrl);   //itmSource.File;
                SPList libDest = webDst.Lists["Lib1"];

                SPUser userCreatedBy = fileSource.Author;
                SPUser userModifiedBy = fileSource.ModifiedBy;
                DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();
                int countVersions = fileSource.Versions.Count;
                for (int i = 0; i <= countVersions; i++)
                {
                    Console.Write("Item Vesrion no :  " + i);
                    Hashtable hashSourceProp;
                    Stream streamFile;
                    //SPUser userModifiedBy;
                    DateTime dateModifiedOn;
                    string strVerComment = "";
                    bool bolMajorVer = false;
                    if (i < countVersions)
                    {
                        SPFileVersion fileSourceVer = fileSource.Versions[i];
                        hashSourceProp = fileSourceVer.Properties;
                        userModifiedBy = (i == 0) ? userCreatedBy : fileSource.Author;
                        dateModifiedOn = fileSourceVer.Created.ToLocalTime();
                        strVerComment = fileSourceVer.CheckInComment;
                        bolMajorVer = fileSourceVer.VersionLabel.EndsWith(".0") ? true : false;
                        streamFile = fileSourceVer.OpenBinaryStream();
                    }
                    else
                    {
                        userModifiedBy = fileSource.ModifiedBy;
                        dateModifiedOn = fileSource.TimeLastModified;
                        hashSourceProp = fileSource.Properties;
                        strVerComment = fileSource.CheckInComment;
                        bolMajorVer = fileSource.MinorVersion == 0 ? true : false;
                        streamFile = fileSource.OpenBinaryStream();
                    }
                    string urlDestFile = fileSource.Url.ToString();
                    SPFile fileDest = libDest.RootFolder.Files.Add(
                                destSiteUrl+ "/File1.xlsx",
                                streamFile,
                                hashSourceProp,
                                libDest.ParentWeb.EnsureUser(userCreatedBy.LoginName),
                                libDest.ParentWeb.EnsureUser(userModifiedBy.LoginName),
                                dateCreatedOn,
                                dateModifiedOn,
                                strVerComment,
                                true);
                    if (bolMajorVer)
                    {
                        fileDest.Publish(strVerComment);
                    }
                    else
                    {
                        SPListItem itmNewVersion = fileDest.Item;
                        itmNewVersion["Created"] = dateCreatedOn;
                        itmNewVersion["Modified"] = dateModifiedOn;
                        itmNewVersion.UpdateOverwriteVersion();
                    }
                }

            }
        }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.