0

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.

fix105
  • 234
  • 1
  • 6
  • 16

2 Answers2

0

Use item.Update() instead of SystemUpdate(). It should retain the identity of logged in user.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • It doesn't change. It's always the System Account. – fix105 Sep 09 '14 at 07:56
  • Reading the MSDN article http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.systemupdate(v=office.15).aspx. It looks like SystemUpdate needs to be used in my case. – fix105 Sep 09 '14 at 11:24
  • I made another test: I have used SPUser author = fileToMove.Author; folder.Files.Add(destUrl, bin,author,author,DateTime.Now,DateTime.Now); instead of folder.Files.Add(destUrl, bin, fileToMove.Properties, true); The result is that the Modified By field is correctly defined but I'm losing the meta-data's selected. – fix105 Sep 10 '14 at 07:20
  • Ok. Not sure what the problem is. See if you can solve it by using alternate elevation mechanism in page below http://www.mssharepointtips.com/tip.asp?id=1022 – Madhur Ahuja Sep 10 '14 at 07:22
  • That's still not working. I made another test, using other properties in the Files.Add function. I made an update in my question. I think i will have to review the code or changing the approach to meet the requirement. Not sure how to do it. – fix105 Sep 11 '14 at 08:23
0

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.

fix105
  • 234
  • 1
  • 6
  • 16