Frage

I want to get author and editor on SP ListItem:

With this Page_load Code :

SPQuery myQuery = new SPQuery();
myQuery.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Text'>" + id + "</Value></Eq></Where>";

SPListItemCollection myItemCol = list.GetItems(myQuery);


foreach (SPListItem item in myItemCol)
{
    author = item["Author"].ToString();
    editor = item["Editor"].ToString();
}

this.Controls.Add(new LiteralControl("<p>" + "Created by : " + author + "</p>"));
this.Controls.Add(new LiteralControl("<p>" + "Edited by: " + editor + "</p>"));

Result is :

enter image description here

When Another user edit an item : Created by must change. I want "Created by" to be a static data.

How to solve this problem?

War es hilfreich?

Lösung

Author is the person who created the item. It is assigned once the item is created and never changed. Editor is changed every time item is modified.

Better use SPFieldUserValue to retrieve SPUser.

foreach (SPListItem item in myItemCol)
{
    SPFieldUserValue usrAuthor = new SPFieldUserValue(SPContext.Current.Web, item["Author"].ToString());
author = usrAuthor.User.Name;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top