how to to display and search between date sharepoint document library in web part
-
18-01-2021 - |
Question
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;
}
Solution
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange