I am reading the sharepoint online list item using SharePointPnPCoreOnline as below. But it doesn't retrieve any items. Data count is always zero. Please let me know what I am missing.
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
string siteUrl = "****";
string clientId = "*****";
string clientSecret = "******";
using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))
{
clientContext.Load(clientContext.Web, p => p.Title);
clientContext.ExecuteQuery();
Console.WriteLine(clientContext.Web.Title);
List sourceList = clientContext.Web.Lists.GetByTitle("MyList");
clientContext.Load(sourceList);
clientContext.ExecuteQuery();
Console.WriteLine("GUID of List: " + sourceList.Id);
CamlQuery camlQuery = new CamlQuery();
ListItemCollection listItems = sourceList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
Console.WriteLine("Total Item Count of List: " + listItems.Count);
foreach (ListItem listItem in listItems)
{
foreach (var field in listItem.FieldValues)
{
Console.WriteLine("{0} : {1}", field.Key, field.Value);
}
}
}
Note: when I try the same set of code with username and password,I am able to retreive the items in the list.
And the Client ID and Secret are created in Azure.
Related
I want to display files from a sharepoint folder that were modified by username. please help me for this. Also tell me how to show this file with sorting order by datetime.
I tried using
docName.Add(file.ModifiedBy);
property but its not available, here is the code:
public List<string> getFiles(ClientContext CContext,string INVOICENO)
{
List list = CContext.Web.Lists.GetByTitle("Documents");
CContext.Load(list);
CContext.Load(list.RootFolder);
CContext.Load(list.RootFolder.Folders);
CContext.Load(list.RootFolder.Files);
CContext.ExecuteQuery();
FolderCollection fcol = list.RootFolder.Folders;
List<string> docName = new List<string>();
foreach (Folder f in fcol)
{
if(INVOICENO==null)
{
INVOICENO = "";
}
string foldername = INVOICENO.ToString();
if (f.Name == foldername)
{
CContext.Load(f.Files);
CContext.ExecuteQuery();
FileCollection fileCol = f.Files;
foreach (File file in fileCol)
{
docName.Add(file.Name);
docName.Add(file.TimeLastModified.ToShortDateString());
}
}
}
return docName.ToList();
}
According to my testing and research, you can use CAML Query to display the files modified by the username from the SharePoint folder.
Here is an example you can refer to:(sorted by modification time in ascending order)
static void Main(string[] args)
{
var clientContext = GetonlineContext();
Web site = clientContext.Web;
List list = clientContext.Web.Lists.GetByTitle("Library Name");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Editor' /><Value Type='User'>UserName</Value></Eq></Where><OrderBy><FieldRef Name='Modified' Ascending='True' /></OrderBy></Query><ViewFields /><QueryOptions /></View>";
ListItemCollection collListItem = list.GetItems(query);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
foreach (ListItem item in collListItem)
{
Debug.WriteLine("Name:"+item["FileLeafRef"]+"\n"+"Modified"+item["Modified"]);
}
clientContext.ExecuteQuery();
}
I have the code below:
using (var context = new ClientContext(URL))
{
context.Credentials = new SharePointOnlineCredentials(username, password);
Web web = context.Web;
context.Load(web.Lists, lists => lists.Include(list => list.Title, list => list.Id));
context.ExecuteQuery();
List SupportList = context.Web.Lists.GetByTitle("Support");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = SupportList.AddItem(itemCreateInfo);
foreach (var selector in f.rtxt_select.Text.Split(','))
{
var s = selector.Split('=');
newItem[s[0].Trim()] = m.GetType().GetProperty(s[1].Trim()).GetValue(m, null);
}
newItem.Update();
context.ExecuteQuery();
f.txtMessage.AppendText("Message has been added to Sharepoint List \n ");
f.lbl_successCount.Text = (Convert.ToInt32(f.lbl_successCount.Text) + 1).ToString();
}
Everything works just fine if the username and password is correct. But Is there is a way i can validate if the user has access to the site(SharePoint Online)?
Check Web.GetUserEffectivePermissions method.
I have this code block in c# vs 2013
using System;
using Microsoft.SharePoint.Client;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace IndicadoresCartera
{
public class SPQuery
{
public ClientContext clientContext;
public Web site;
public ListItemCreationInformation itemCreateInfo;
static SPQuery queryService = new SPQuery();
public SPQuery()
{
string url = ConfigurationManager.AppSettings["url"];
string user = ConfigurationManager.AppSettings["user"];
string password = ConfigurationManager.AppSettings["password"];
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
clientContext = new ClientContext(url);
clientContext.Credentials = new SharePointOnlineCredentials(user, securePassword);
site = clientContext.Web;
itemCreateInfo = new ListItemCreationInformation();
}
public ListItemCollection queryList(string list, string queryString)
{
List queriedList = clientContext.Web.Lists.GetByTitle(list);
CamlQuery query = new CamlQuery();
query.ViewXml = queryString;
ListItemCollection items = queriedList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
return items;
}
public bool createItem(string List, ListItem newListItem)
{
Clientes inCartera = new Clientes();
List targetList = site.Lists.GetByTitle(List);
newListItem.Update();
clientContext.Load(newListItem);
try
{
clientContext.ExecuteQuery();
return true;
}
catch (Exception e)
{
Console.WriteLine("error creando item: " + e.Message);
return false;
}
}
public void getClientes()
{
List clientesGet = clientContext.Web.Lists.GetByTitle("clients");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><ViewFields><FieldRef Name='ID' /></ViewFields></View>";
ListItemCollection ToDoClientes = clientesGet.GetItems(query);
clientContext.Load(ToDoClientes);
clientContext.ExecuteQuery();
foreach (var cl in ToDoClientes.ToList())
{
if (cl["Title"] != null && cl["code"] != null && cl["name"] != null)
{
List targetList = queryService.site.Lists.GetByTitle("Indi");
ListItem inCartera = targetList.AddItem(queryService.itemCreateInfo);
inCartera["Proceso"] = "Cliente";
inCartera["IdProceso"] = cl.Id;
inCartera["Responsable"] = "Cartera";
inCartera["Rol"] = "Cartera";
inCartera["Inicio"] = cl["FechaSolicitud"];
inCartera["Fin"] = cl["FechaAsignacionCodigo"];
inCartera["EstadoFinal"] = "Cliente Creado";
queryService.createItem("indicators", inCartera);
}
}
}
}
}
What it does is consult the clients list and then with the if I tell it which one it inserts in the list of indicators.
The problem is that the list of clients has more than 2,000 records and how is online sharepoint I get an error
of information:
An operation that is prohibited was attempted because it exceeds the list view threshold applied by the administrator
How can I consult the list so that it triagates all the data and with the the if filter?
I am using the following code to get users from a SharePoint list:
private ClientContext clientContext = new ClientContext(siteUrl);
private SP.List oList = clientContext.Web.Lists.GetByTitle("SharePoint List");
private CamlQuery camlQuery = new CamlQuery();
private ListItemCollection collListItem = oList.GetItems(camlQuery);
private ArrayList names = new ArrayList();
clientContext.Load(collListItem, items => items.Include(
item => item["UserNames"]));
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
titles.Add(oListItem["UserNames"]);
}
I am retrieving data from another columns, too, and I get those data just fine. But when it comes to names, the return value is the Microsoft.SharePoint.Client.FieldUserValue.
Any suggestions on how to get the actual usernames?
It's supposed to return FieldUserValue, you can get the users name or ID from the object. Here's a quick example:
FieldUserValue user = (FieldUserValue)listItem["Author"];
string name = user.LookupValue;
I want to read a specific SharePoint list into a datatable. I am struggeling to read the Lookup Values.
Example:
Customer list, each customer is assigned to a team member. The Team members are a lookup of the Team member list.
I have the following questions:
1.) How can I get the Information which list is linked to this lookup field?
2.) How can read the field, gets the value or the index?
I added my function below, many thanks for helping a desperate novice in SharePoint...
Regards
Volker
public System.Data.DataTable ReadDatafromSPList(ClientContext clientContext, string ListName, string ViewName)
{
Web site = clientContext.Web;
List list = site.Lists.GetByTitle(ListName);
View view = list.Views.GetByTitle(ViewName);
ViewFieldCollection viewFields = view.ViewFields;
clientContext.Load(view);
clientContext.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = view.ViewQuery;
ListItemCollection ListColletion = list.GetItems(camlQuery);
clientContext.Load(list);
clientContext.Load(ListColletion);
clientContext.Load(viewFields);
clientContext.ExecuteQuery();
string[] headers = view.ViewFields.ToArray();
System.Data.DataTable dTable = new System.Data.DataTable();
foreach (string header in headers)
{
dTable.Columns.Add(header);
}
string str="";
foreach (ListItem item in ListColletion)
{
dTable.Rows.Add(dTable.NewRow());
foreach (string header in headers)
{
try
{
str = item[header].ToString();
if (str == "Microsoft.SharePoint.Client.FieldLookupValue")
{
//??????????????????
//??????????????????
}
dTable.Rows[dTable.Rows.Count - 1][header] = str;
}
catch
{
dTable.Rows[dTable.Rows.Count - 1][header] = "";
}
}
}
return dTable;
}
Do try this..
ClientContext context = new ClientContext(ServerText.Text);
List list = context.Web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);
context.Load(list);
context.Load(items);
context.ExecuteQuery();
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
foreach (ListItem item in items)
table.Rows.Add(item.Id, item["Title"]);