Thursday, 24 July 2014

update the values of “Created By”, “Modified By” columns in SharePoint lists

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SPQuery oquery = null;
SPSite siteCollection = null;
SPWeb topLevelSite = null;
SPListItemCollection myitems = null;
try
{
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPSite"]);
topLevelSite = siteCollection.AllWebs[System.Configuration.ConfigurationManager.AppSettings["TopLevelSite"]];
SPList list = topLevelSite.Lists[System.Configuration.ConfigurationManager.AppSettings["ListName"]];
topLevelSite.AllowUnsafeUpdates = true;
//Create an instance of SPQuery Object
oquery = new SPQuery();
oquery.ViewFields = "";
DataTable dtNewItems;
myitems = list.Items;
dtNewItems = myitems.GetDataTable();
ddlListId.DataSource = dtNewItems;
ddlListId.DataTextField = "ID";
ddlListId.DataValueField = "ID";
ddlListId.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
topLevelSite.AllowUnsafeUpdates = false;
siteCollection.Dispose();
topLevelSite.Dispose();
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (ddlListId.SelectedItem.Value != "")
{
SPSite siteCollection = null;
SPWeb topLevelSite = null;
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPSite"]);
topLevelSite = siteCollection.AllWebs[System.Configuration.ConfigurationManager.AppSettings["TopLevelSite"]];
SPList list = topLevelSite.Lists[System.Configuration.ConfigurationManager.AppSettings["ListName"]];
topLevelSite.AllowUnsafeUpdates = true;
SPListItemCollection oListCollection = list.Items;
foreach (SPListItem oListItem in oListCollection)
{
if (Convert.ToString(oListItem["ID"]) == ddlListId.SelectedItem.Value)
{
//SPFieldUserValue oUserAss = new SPFieldUserValue(topLevelSite, topLevelSite.CurrentUser.ID, topLevelSite.CurrentUser.LoginName);
//SPFieldUserValue oUserAss = new SPFieldUserValue(topLevelSite, int.Parse(ddlListId.SelectedItem.Value), txtAssignedTo.Text);
SPUser spUserAssgned = GetSPUser(txtAssignedTo.Text, System.Configuration.ConfigurationManager.AppSettings["SPSite"]);
//oUserAss.User = spUser;
// or you can hard code the value like this,
//SPFieldUserValue oUserCre = new SPFieldUserValue(topLevelSite, int.Parse(ddlListId.SelectedItem.Value), txtCreBy.Text);
SPUser spUserCre = GetSPUser(txtCreBy.Text, System.Configuration.ConfigurationManager.AppSettings["SPSite"]);
//oUserCre.User = txtCreBy.Text;
oListItem["Assigned_x0020_To"] = spUserAssgned;
oListItem["Author"] = spUserCre;
oListItem.Update();
}
}
topLevelSite.Update();
}
else
{
lblError.Text = "Please select list id";
}
}
private SPUser GetSPUser(string strLoginName, string strSiteURL)
{
SPUser spReturn = null;
SPSite spSite = null;
SPWeb spWeb = null;
try
{
//Open the ShrePoint site
spSite = new SPSite(strSiteURL);
spWeb = spSite.OpenWeb();
//Check to see if user exists
spReturn = spWeb.AllUsers[txtAssignedTo.Text];
}
catch (Exception)
{
}
finally
{
spWeb.Close();
spSite.Close();
}
return spReturn;
}
}
------------------------------
null