Search code examples
asp.netsharepointsharepoint-2010sharepoint-2007

Employee ID reflected in created by column instead of display name


I wrote a program to import data from excel sheet to sharepoint 2007 list. Around 11000 data gets imported. I have used the below code. My query is I wanted to put "employee's display name" in "created by" column. And im providing the same in excel sheet n in the code. But after the data gets imported I see that few employees data has reflected the created by column with their names. But for few it reflects EMPID only or EMPID + name. I debug the code It takes the right string to display but i did not understand y it gives such results. Also i am running the prog on my machine n not the server, so is it bcoz of this. As I am using test server and then only will deploy to production.

Code:

protected void btnImport_Click(object sender, EventArgs e)

        {



            using (SPSite site = new SPSite("URL"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    webapp = web.Site.WebApplication;

                    webapp.FormDigestSettings.Enabled = false;

                    SPList list = web.Lists["List name"];



                    string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Test.xlsx;Extended Properties=Excel 12.0";

                    OleDbConnection oledbConn = new OleDbConnection(connString);

                    oledbConn.Open();

                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);//contents from sheet1 is selected

                    OleDbDataAdapter oleda = new OleDbDataAdapter();

                    oleda.SelectCommand = cmd;

                    DataSet ds = new DataSet();

                    oleda.Fill(ds, "Employees");

                    DataTable dt = ds.Tables["Employees"];

                    DataView dv = new DataView(dt);

                    SPSecurity.RunWithElevatedPrivileges(delegate()

                    {

                        using (SPSite elevatedSite = new SPSite("URL"))

                        {

                            elevatedRootWeb = elevatedSite.OpenWeb();

                        }

                    });

                    foreach (DataRowView drv in dv)

                    {

                        EMPID = drv["Emp id"].ToString();

                        DispName = drv["Name"].ToString();

                        Title = drv["Title"].ToString();

                        getid = new SPQuery();

                        getid.Query = "<Where><Eq><FieldRef Name=’EMPID’ /><Value Type='Text'>" + EMPID + "</Value></Eq></Where><OrderBy><FieldRef Name='ID'/></OrderBy>";

                        check = list.GetItems(getid).GetDataTable();

                        if (check == null)

                        {

                                try

                                {





                                    elevatedRootWeb.AllowUnsafeUpdates = true;

                                    UserItem = list.Items.Add();

                                    UserItem["Emp id"] = EMPID;

                                    UserItem["Title"] = Title;

                                    test = elevatedRootWeb.EnsureUser(PSNumber).ID + ";#" + DispName;

                                    UserItem["Author"] = test;

                                    UserItem.Update();

                                    list.Update();

                                    count++;

                                    elevatedRootWeb.AllowUnsafeUpdates = false;



                                    using (StreamWriter w = File.AppendText("D:\\Errorlog_SP2010.txt"))

                                    {

                                        Log(PSNumber + "Inserted successfully", w);



                                        w.Close();

                                    }

                                }

                                catch (Exception ex)

                                {

                                    HttpContext.Current.Response.Write("<script>alert('Exception on adding item " + ex.Message + "')</script>");

                                    using (StreamWriter w = File.AppendText("D:\\Errorlog_SP2010.txt"))

                                    {

                                        Log(ex.ToString()+ PSNumber, w);



                                        w.Close();

                                    }


                                }

                        }

Solution

  • check the "_catalogs/users/simple.aspx" list to see if the user on that site has thier information stored correctly.

    This is important as the Author field takes it's value from what is displayed in this list, not exactly what you set.

    See this question on how to fix this.