0

hi every one, i am trying to display all document library item in gridview using web part with a linkFileName column. below is my code.

try
{
    using (ClientContext context = new ClientContext("URL"))
    {
        context.AuthenticationMode = ClientAuthenticationMode.Default;
        context.Credentials = new NetworkCredential("Username", "Password");
        Web webObj = context.Web;
        List listObj = webObj.Lists.GetByTitle("ListName");
        CamlQuery camlquery = CamlQuery.CreateAllItemsQuery(1000000);
        ListItemCollection listitemcoll = listObj.GetItems(camlquery);
        context.Load(listitemcoll);
        context.ExecuteQuery();

        //4/25/2013 12:00:00 PM
        if (listitemcoll.Count != 0)
        {
            DataTable dt = new DataTable();
            DataRow dRow;

            DataColumn dcNotificationNumber = new DataColumn("Notification_x0020_Number");
            dcNotificationNumber.DataType = Type.GetType("System.String");

            DataColumn dcSubject = new DataColumn("Subject");
            dcSubject.DataType = Type.GetType("System.String");

            DataColumn dcCreated = new DataColumn("Created");
            dcCreated.DataType = Type.GetType("System.String");
            DataColumn dclink = new DataColumn("LinkFilename");
            dclink.DataType = Type.GetType("System.String");

            dt.Columns.Add(dcNotificationNumber);
            dt.Columns.Add(dcSubject);
            dt.Columns.Add(dcCreated);
            dt.Columns.Add(dclink);

            foreach (ListItem item in listitemcoll)
            {
                dRow = dt.NewRow();
                dRow["Notification_x0020_Number"] = item["Notification_x0020_Number"];
                dRow["Subject"] = item["Subject"];
                dRow["Created"] = item["Created"];
                //string linkFileName = file.Item["LinkFilename"] as string;
                // dRow["LinkFilename"] = item["LinkFilename"];
                string fileurl = (string)item["FileRef"];
                dRow[fileurl]=item[fileurl];
                dt.Rows.Add(dRow);
            }
            dataGridView1.DataSource = dt;
        }
        else
        {
            //write messages to client - no data is present
        }
    }
}
catch (Exception ex)
{
    throw ex;
}
7
  • What is the problem with your code?
    – helb
    Jun 14 '18 at 6:34
  • You are not actually using "URL" as parameter to ClientContext() right?
    – helb
    Jun 14 '18 at 6:40
  • Hi the gridview is of Window form based application or web based application?
    – Sunil Sahu
    Jun 14 '18 at 11:52
  • @helb the problem is when i want to display a linkfile name it throws error
    – Razim Khan
    Jun 20 '18 at 6:12
  • @Sunilsahu it is webpart , web base.
    – Razim Khan
    Jun 20 '18 at 6:13
0

hi updated the code try to verify

try
{
using (ClientContext context = new ClientContext("URL"))
{
    context.AuthenticationMode = ClientAuthenticationMode.Default;
    context.Credentials = new NetworkCredential("Username", "Password");
    Web webObj = context.Web;
    List listObj = webObj.Lists.GetByTitle("ListName");
    CamlQuery camlquery = CamlQuery.CreateAllItemsQuery(1000000);
    ListItemCollection listitemcoll = listObj.GetItems(camlquery);
    context.Load(listitemcoll);
    context.ExecuteQuery();

    //4/25/2013 12:00:00 PM
    if (listitemcoll.Count != 0)
    {
        DataTable dt = new DataTable();
        DataRow dRow;

        DataColumn dcNotificationNumber = new DataColumn("Notification_x0020_Number");
        dcNotificationNumber.DataType = Type.GetType("System.String");

        DataColumn dcSubject = new DataColumn("Subject");
        dcSubject.DataType = Type.GetType("System.String");

        DataColumn dcCreated = new DataColumn("Created");
        dcCreated.DataType = Type.GetType("System.String");
        DataColumn dclink = new DataColumn("LinkFilename");
        dclink.DataType = Type.GetType("System.String");

        dt.Columns.Add(dcNotificationNumber);
        dt.Columns.Add(dcSubject);
        dt.Columns.Add(dcCreated);
        dt.Columns.Add(dclink);

        foreach (ListItem item in listitemcoll)
        {
            dRow = dt.NewRow();
            dRow["Notification_x0020_Number"] = item["Notification_x0020_Number"];
            dRow["Subject"] = item["Subject"];
            dRow["Created"] = item["Created"];
            //string linkFileName = file.Item["LinkFilename"] as string;
            // dRow["LinkFilename"] = item["LinkFilename"];
            //string fileurl = (string)item["FileRef"];
            dRow["LinkFilename"]=Convert.ToString(item["FileRef"]);
            dt.Rows.Add(dRow);
        }
        dataGridView1.DataSource = dt;

 //If it is gridview and not datagrid then uncomment below line
//dataGridView1.Databind();
    }
    else
    {
        //write messages to client - no data is present
    }
}
}
catch (Exception ex)
{
  throw ex;
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.