CreatedBy name in SharePoint list item

 private void btnGetDetails_Click(object sender, EventArgs e)
        {
            txtSiteUrl.Text = "http://SP2010:8080/sites/TestSite";
            txtListName.Text = "Employee";

            ListItemCollection itemCollection = GetData(txtSiteUrl.Text, txtListName.Text);
            DataTable table = new DataTable();

            table.Columns.Add("ProjectNO");//projectno
            table.Columns.Add("CreatedBy");//Author
            table.Columns.Add("Primary Administrator");//Primary_x0020_Administrator
            table.Columns.Add("CreatedDate");//Created

            MessageBox.Show("Count:" + itemCollection.Count);

            foreach (ListItem item in itemCollection)
            {
                var Authorname = (FieldUserValue)(item["Author"]);
                var PrimaryAdmin = (FieldUserValue)(item["Primary_x0020_Administrator"]);

                string projectNo = item["Title"].ToString();
                string author = Authorname.LookupValue;
                string primaryadmin = PrimaryAdmin.LookupValue;
                string createdDate = item["Created"].ToString();

                table.Rows.Add(projectNo, author, primaryadmin, createdDate);
                
            }
            DataView dv = table.DefaultView;
            dv.Sort = "ProjectNo asc";
            gdView.DataSource = dv.ToTable();
        }

        public ListItemCollection GetData(string siteURL, string listName)
        {
            ClientContext context = new ClientContext(siteURL);
            List myCustomList = context.Web.Lists.GetByTitle(listName);
            CamlQuery query = new CamlQuery(); query.ViewXml = "<View/>";
            ListItemCollection listItemCollection = myCustomList.GetItems(query);
            context.Load(listItemCollection, items => items.Include(item => item["Title"],
                                                                   item => item["Author"],
                                                                   item => item["Primary_x0020_Administrator"],
                                                                   item=>item["Created"]));
           context.ExecuteQuery();
            context.Dispose();

            return listItemCollection;
        }

Comments

Popular posts from this blog

Sending email using Powershell script

Convert List Collection to DataTable using C#

Difference Between Site Template and Site Definition