Search code examples
sharepointsharepoint-2013spsite

Renamed the "Modify By" field after using Elevated Mode


I'm currently facing to an issue using the Elevated Mode used in a Feature.

I have created a custome SharePoint security role (contribute role without the Delete right). The goal of my SharePoint feature is the following:

When uploading a file to a SP Site, the name of the file needs to be renamed using the meta-data's selected. When uploading a file, a second form is asking the user to defined 3 or 4 meta-data's.

To rename the file, i have developed the following code:

    Public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
       {
CallFunctionToUpdate();
});
}


Public override void ItemUpdated(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
       {
CallFunctionToUpdate();
});
}



Public void CallFunctionToUpdate()
{
try
       {
        this.EventFiringEnabled = false;
              using (SPWeb newWeb = newSite.OpenWeb(_properties.RelativeWebUrl))
              {
SPListItem item = newWeb.Lists[_properties.ListId].GetItemById(_properties.ListItem.ID);
newFileName = FilenameManagementHelper.GenerateFilename(properties.ListItem);

                     currentName = properties.ListItem["Name"].ToString();
                     var extension = Path.GetExtension(currentName);

                     if (item["Title"] == null || item["Title"].ToString() != newFileName)
                     {
                        item["Title"] = newFileName;
item["Editor"] = new SPFieldUserValue(properties.OpenWeb(), properties.OpenWeb().CurrentUser.ID, properties.OpenWeb().CurrentUser.LoginName);
                            item.SystemUpdate();
                     }

                     if (currentName.Substring(0, currentName.Length - extension.Length) != newFileName)
                     {
                        SPList list = newWeb.Lists[_properties.List.Title];
                           string destUrl = list.RootFolder.Url + "/" + newFileName + extension;
SPFile fileToMove = list.Items[_properties.ListItemUniqueId].File;

                           SPFolder folder = newWeb.GetFolder(list.RootFolder.Url);
                           byte[] bin = fileToMove.OpenBinary();
                           folder.Files.Add(destUrl, bin, fileToMove.Properties, true);

fileToMove.Delete();                             newWeb.Lists[list.RootFolder.Url].Update();
                        }

                     }
}
       catch (Exception ex)
       {
           DocumentDiagnosticService.LogError(CategoryID.Error, string.Format("Error {0} for the file name - {1}", ex.Message, currentName));
}
       finally
       {
        this.EventFiringEnabled = true;
       }

}

Before renaming the file name, I'm updating the field (meta-data's) title and Editor. The second step will move the file (with the meta-data's and the history associated to the uploaded file)

I'm using the Elevated Mode because the user with a restricted Security role cannot delete. In the code developed I'm moving the file renamed and deleting the old file uploaded.

I found that approach because I need to keep the versioning. Updating directly the name of the file (like for the title) is not allowed and that's losing the history. Ex: A file will be uploaded, the name of the file will be updated using the Meta-data's. For the first version, there is no issue. Uploading a second file with the same meta-data's as there is already an existing file with the same name, that will generate an error. Using the Files.Add, that will oerride the current file and will keep the history.

My issue in this case: When the user is uploading the file, the fields Title and Editor are correctly replaced. Than is moving the file, renaming the field Name and deleting the old version. At this moment, the Modify by field is coming the SharePoint Sys Admin all the time.

How can i keep the Modifiy by with the name of the person who is uploading the file ?

EDIT:

Using the following code:

SPList list = newWeb.Lists[_properties.List.Title];
string destUrl = list.RootFolder.Url + "/" + newFileName + extension;
SPFile fileToMove = list.Items[_properties.ListItemUniqueId].File;
SPFolder folder = newWeb.GetFolder(list.RootFolder.Url);
byte[] bin = fileToMove.OpenBinary();
folder.Files.Add(destUrl, bin, fileToMove.Properties, true);
fileToMove.Delete();     

Allow me to move the file with the meta-data's selected during the upload. I still have the versioning if a current version is already uploaded BUT the Modified By is SysAdmin.

Using the following code:

SPList list = newWeb.Lists[_properties.List.Title];
string destUrl = list.RootFolder.Url + "/" + newFileName + extension;
SPFile fileToMove = list.Items[_properties.ListItemUniqueId].File;
SPFolder folder = newWeb.GetFolder(list.RootFolder.Url);
byte[] bin = fileToMove.OpenBinary();
SPUser author = fileToMove.Author;
folder.Files.Add(destUrl, bin, author, author, DateTime.Now, DateTime.Now);
fileToMove.Delete();                          

Allow me to move the file and keep the history if i already have a version. I can now get the Modified By field filled by the real user who is uploading and not the SysAdmin BUT I'm losing the meta-data's selected during the upload.

Thank you for your support,

Fix.


Solution

  • Thank you for your support. I have solved my issue by using the following code:

    folder.Files.Add(destUrl, bin, fileToMove.Properties, author, author, DateTime.Now, DateTime.Now, true);
    

    Now, I have the Modified By value filled with the user who is uploading, the meta-data's are still there and the versioning too.