How do I update a Sharepoint ListItem with .Net? - c#

Basically I query Sharepoint and get a list of ListItems. I foreach through the list and check to see if the item needs to be updated from an external db (that code is not present)
Here is the bit of code that I was running that would not update the Sharepoint ListItem. I even tried different credentials to no avail.
using(ClientContext ctx = new ClientContext(searchsiteurl)) {
//NetworkCredential credit = new NetworkCredential(prg.userName, prg.password, prg.domain);
//ctx.Credentials = credit;
Web web = ctx.Web;
List list = web.Lists.GetById(new Guid(site.ListGUID));
var q = new CamlQuery();
if (Fullsync) {
q.ViewXml = "<View><Query><Where><And><BeginsWith><FieldRef Name='SrNumber' /><Value Type='Text'>1</Value></BeginsWith>" + "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Draft</Value></Eq></And></Where></Query></View>";
}
else {
q.ViewXml = "<View><Query><Where><And><Contains><FieldRef Name='SrNumber' /><Value Type='Text'>1-</Value></Contains><And>" + "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq>" + "<Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + last24hours + "</Value></Gt>" + "</And></And></Where></Query></View>";
}
var r = list.GetItems(q);
ctx.Load(r);
ctx.ExecuteQuery();
foreach(SP.ListItem lit in r) {
//do a whole bunch of stuff....
// this does NOT WORK
lit.FieldValues["Linked_x0020_CSRs"] = LinkedSRs;
lit.Update();
ctx.ExecuteQuery();
}
}

The problem with the documentation is that it does not explicitly indicate that you must use the .GetItemById() function to retrieve a ListItem in order to update that ListItem.
So here is the code that should help out future people searching for this answer. It took me waaaay too long to figure this out.
using (ClientContext ctx = new ClientContext(searchsiteurl))
{
//NetworkCredential credit = new NetworkCredential(prg.userName, prg.password, prg.domain);
//ctx.Credentials = credit;
Web web = ctx.Web;
List list = web.Lists.GetById(new Guid(site.ListGUID));
var q = new CamlQuery();
if (Fullsync)
{
q.ViewXml = "<View><Query><Where><And><BeginsWith><FieldRef Name='SrNumber' /><Value Type='Text'>1</Value></BeginsWith>" +
"<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Draft</Value></Eq></And></Where></Query></View>";
}
else
{
q.ViewXml = "<View><Query><Where><And><Contains><FieldRef Name='SrNumber' /><Value Type='Text'>1-</Value></Contains><And>" +
"<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq>" +
"<Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + last24hours + "</Value></Gt>" +
"</And></And></Where></Query></View>";
}
var r = list.GetItems(q);
ctx.Load(r);
ctx.ExecuteQuery();
foreach (SP.ListItem lit in r)
{
//do a whole bunch of stuff....
/* this does NOT WORK
lit.FieldValues["Linked_x0020_CSRs"] = LinkedSRs;
lit.Update();
ctx.ExecuteQuery();
*/
// this works!
var KAToModify = list.GetItemById(lit.Id);
KAToModify["Linked_x0020_CSRs"] = LinkedSRs;
KAToModify.Update();
ctx.ExecuteQuery();
}
}

it Won`t work you Can use this Enumerator Method
enter code here
foreach (var i in cv)
{
var items = new ListItemCreationInformation();
var item = lists.AddItem(items);
item["Title"] = i.Title;
item.Update();
ctx.Load(item);
ctx.ExecuteQuery();
}`
you will get all your list item you this
if you want particular item in list
you use GetById()

Related

ALL employees under a director or above using Active Directory and c#

I'm attempting to create a cascading drop down in which the first populates a list of Director level employees (directors have multiple managers and the managers have multiple employees) which would then populate the second dropdown through ajax. I'm trying to put together the logic that given a displayname will grab all direct reports and the direct reports of the direct reports. With the logic I've put together it will print everyone im looking for but only returns the reports to the original Director. I'm struggling to put everything together. The code isnt at all pretty and if there's an easier way to do this I'm open for suggestions.
There are three methods in which I'm attempting to populate the directReports list in the DirectReports method with all employees
public List<string> DirectReports(string name)
{
List<string> directReports = new List<string>();
var domain = new PrincipalContext(ContextType.Domain, "somedomain");
UserPrincipal user = new UserPrincipal(domain);
user.DisplayName = name;
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = user;
DirectorySearcher ds = ps.GetUnderlyingSearcher() as DirectorySearcher;
ds.SearchScope = SearchScope.Subtree;
ds.Filter = "(&(objectClass=user)(objectCategory=person)(displayName=" + name + "))";
ds.PropertiesToLoad.Add("DirectReports");
SearchResultCollection results = ds.FindAll();
foreach(SearchResult result in results)
{
if (hasDirectReports(result))
{
//directReports.AddRange(GetReportsFinal(GetDirectReportsList(result)));
directReports.AddRange(GetDirectReportsList(result));
directReports.Add(result.Properties["displayname"][0].ToString() + " (" + result.Properties["samaccountname"][0].ToString()+")");
}
else
{
directReports.Add(result.Properties["displayname"][0].ToString() + " (" + result.Properties["samaccountname"][0].ToString() + ")");
}
}
foreach(var item in directReports)
{
Debug.WriteLine(item);
}
return directReports;
}
public bool hasDirectReports(SearchResult result)
{
if(result.Properties["directreports"].Count >= 1)
{
return true;
}
else
{
return false;
}
}
public List<string> GetDirectReportsList(SearchResult result)
{
List<string> formattedDirectReportsList = new List<string>();
foreach (var thing in result.Properties["directreports"])
{
string employeeString = thing.ToString();
//employeeString = employeeString.Split(setp, StringSplitOptions.None)[0];
employeeString = employeeString.Replace("CN=", "");
employeeString = employeeString.TrimEnd(',');
employeeString = employeeString.Replace("\\, ", ", ");
//Debug.WriteLine(employeeString);
if (employeeString.Split(',')[2] == "OU=Users")
{
string fn = employeeString.Split(',')[1];
string ln = employeeString.Split(',')[0];
var domain = new PrincipalContext(ContextType.Domain, "somedomain");
UserPrincipal directreportname = new UserPrincipal(domain);
directreportname.Name = ln + "," + fn;
PrincipalSearcher prinsearcher = new PrincipalSearcher();
prinsearcher.QueryFilter = directreportname;
DirectorySearcher dirsearcher = prinsearcher.GetUnderlyingSearcher() as DirectorySearcher;
Principal reportResults = prinsearcher.FindOne();
formattedDirectReportsList.Add(reportResults.DisplayName + " (" + reportResults.SamAccountName + ")");
DirectReports(reportResults.DisplayName);
}
}
return formattedDirectReportsList;
}

caml query not working properly in sharepoint online

I need to get list items which is older than say 7 days and delete them. I tried using caml query and it worked well in sharepoint 2010 but when I tried to use the same in Sharepoint Online, its getting all list items and deleting it regardless of the condition.
public static bool removeOldEntries(string listName, int offset)
{
bool successFlag = true;
try
{
using (var context = new ClientContext(siteURL))
{
SecureString password = ToSecureString(pwd);
context.Credentials = new SharePointOnlineCredentials(userName, password);
Web web = context.Web;
var list = context.Web.Lists.GetByTitle(listName);
if (list != null)
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<Where><Leq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='-" + offset + "'/></Value></Leq></Where>";
ListItemCollection collListItem = list.GetItems(camlQuery);
context.Load(collListItem, items => items.Include(
item => item["ID"]));
context.ExecuteQuery();
if (collListItem.Count > 0)
{
foreach (ListItem oListItem in collListItem)
{
ListItem itemToDelete = list.GetItemById(int.Parse(oListItem["ID"].ToString()));
itemToDelete.DeleteObject();
context.ExecuteQuery();
}
}
}
}
}
catch (Exception ex)
{
successFlag = false;
}
return successFlag;
}
Thanks in advance for any help.
First try adding a Tag in your view xml
So it should look like
camlQuery.ViewXml = "<Query><Where><Leq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='-" + offset + "'/></Value></Leq></Where></Query>";
If it doesn't help try adding a view tag
camlQuery.ViewXml = "<View><Query><Where><Leq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='-" + offset + "'/></Value></Leq></Where></Query></View>";
you can make use of the PNP.PowerShell Module.
$CreationDate = Get-Date "16.06.2021 20:04" -Format s
Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Eq></Where></Query></View>"
For more check out the reference:
https://sposcripts.com/sharepoint/sharepointonline/filtering-for-sharepoint-items-with-caml-queries/#If_DateTime_should_exactly_match_a_specific_date

WIQL Query not including "System.AssignedTo" Field

I have this WIQL, who's purpose is to find the user assigned to the work item.
var wiql = string.Format("SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]" +
" FROM WorkItems" +
" WHERE ([System.TeamProject] = '{0}')" +
" AND ([System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug')" +
" ORDER BY [System.Id]", projectName);
I'm executing it as so...
Wiql wiql = new Wiql() { Query = wiqlQueryString };
using (var workItemTrackingHttpClient = new WorkItemTrackingHttpClient(VstsAccess.AccessUri, VstsAccess.AccessCredentials))
{
var workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;
if (workItemQueryResult != null && workItemQueryResult.WorkItemRelations.Any())
{
List<int> sourceIdList = new List<int>();
foreach (var item in workItemQueryResult.WorkItemRelations)
sourceIdList.Add(item.Target.Id);
int[] arr = sourceIdList.ToArray();
string[] fields = { "System.Id", "System.WorkItemType", "System.AssignedTo", "System.Title", "System.Description", "System.State", "System.IterationPath", "System.TeamProject", "System.ChangedDate", "System.ChangedBy", "System.CreatedDate" };
return workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;
}
else
return new List<WorkItem>();
}
But the "AssignedTo" and "Description" fields are not showing up in the work items' dictionary field-set. Why is this so and how can I fix this?
The query results will only contain fields that are non-null, i.e. nobody is assigned to the work item, the field won't be in the Fields dictionary at all.
You need to implement a custom check for those fields and assign them to something according to your logic:
int[] arr = ids.ToArray();
string[] fields = new string[] {
"System.Id",
"System.Title",
"System.State",
"System.AssignedTo"
};
var workItems = await workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf);
List<WorkItemData> list = new List<WorkItemData>();
foreach (var workItem in workItems)
{
var wi = new WorkItemData(workItem.Id.Value);
wi.Title = workItem.Fields["System.Title"].ToString();
wi.State = workItem.Fields["System.State"].ToString();
wi.AssignedTo = workItem.Fields.ContainsKey("System.AssignedTo") ? workItem.Fields["System.AssignedTo"].ToString() : "";
list.Add(wi);
}
You could use the code below to query out workitems and it has "AssignedTo" and "Description" field values.
WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();
string queryString = string.Format("SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]" +
" FROM WorkItems" +
" WHERE ([System.TeamProject] = '{0}')" +
" AND ([System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug')" +
" ORDER BY [System.Id]", "Mtt-Scrum"); ;
// Create and run the query.
Query query = new Query(workItemStore, queryString);
WorkItemCollection witCollection = query.RunQuery();
foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem in witCollection)
{
......
}

How to retrieve list items from SharePoint 2013

At work we had to move from SharePoint 2010 to 2013 and my code for retrieving list items doesn't work anymore. Here is my code for SP 2010:
com.mycompany.intranet.Lists listService = new com.mycompany.intranet.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = "url";
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
string listName = cbxMailDistributionList.SelectedValue.ToString();
string viewName = "";
string rowLimit = "0";
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
viewFields.InnerXml = "<FieldRef Name='Title' />";
System.Xml.XmlNode nodeListItems =
listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
xmlDoc.LoadXml(nodeListItems.InnerXml);
xlNodeList rows = xmlDoc.GetElementsByTagName("z:row");
List<string> recipients = new List<string>();
foreach (XmlNode attribute in rows)
{
if(attribute.Attributes["ows_Title"].Value == null){}
else {
if (recipients.Contains(attribute.Attributes["ows_Title"].Value)){}
else {
recipients.Add(attribute.Attributes["ows_Title"].Value);
}
}
}
recipients.Sort();
distributionList = recipients;
Can you please help me to get it working with a SharePoint 2013 list again?
URL has already been updated but i get the following error: https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=DE-DE&k=k(EHNullReference);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);k(DevLang-csharp)&rd=true
But the list has no empty fields.
listName
is the ID of the list element.
Please help.
Thanks in advance!
Finally it works again with this code:
List<string> recipients = new List<string>();
string siteURL = #"myurl/";
ClientContext cc = new ClientContext(siteURL);
cc.Credentials = System.Net.CredentialCache.DefaultCredentials;
Web web = cc.Web;
List list = web.Lists.GetById(new Guid(cbxMailDistributionList.SelectedValue.ToString()));
CamlQuery caml = new CamlQuery();
ListItemCollection items = list.GetItems(caml);
cc.Load<List>(list);
cc.Load<ListItemCollection>(items);
cc.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem item in items)
{
if(item.FieldValues["Title"] == null) { }
else
{
if (recipients.Contains(item.FieldValues["Title"].ToString())) { }
else
{
recipients.Add(item.FieldValues["Title"].ToString());
}
}
}
recipients.Sort();
distributionList = recipients;
}
else
{
distributionList = null;
}
New day, new luck. Sorry for the prematurely posting of this question. I should have slept on it for a night. ;)
BR

Getting Project List via Rally C# API

I need a way to retrieve a list of projects using C#.
Tried doing something like this:
DynamicJsonObject sub = restApi.GetSubscription("Projects");
//query the project collection
Request wRequest = new Request(sub["Projects"]);
QueryResult queryResult = restApi.Query(wRequest);
return queryResult.Results.Select(result => new Project()
{
Id = result["ObjectID"],
Name = result["Name"]
}).ToList();
unfortunately with no success.
Can anyone help please?
The code below should print workspaces and projects to which the user whose account is used to authenticate the code has access to.
DynamicJsonObject sub = restApi.GetSubscription("Workspaces");
Request wRequest = new Request(sub["Workspaces"]);
wRequest.Limit = 1000;
QueryResult queryResult = restApi.Query(wRequest);
int allProjects = 0;
foreach (var result in queryResult.Results)
{
var workspaceReference = result["_ref"];
var workspaceName = result["Name"];
Console.WriteLine("Workspace: " + workspaceName);
Request projectsRequest = new Request(result["Projects"]);
projectsRequest.Fetch = new List<string>()
{
"Name"
};
projectsRequest.Limit = 10000; //project requests are made per workspace
QueryResult queryProjectResult = restApi.Query(projectsRequest);
int projectsPerWorkspace = 0;
foreach (var p in queryProjectResult.Results)
{
allProjects++;
projectsPerWorkspace++;
Console.WriteLine(projectsPerWorkspace + " Project: " + p["Name"] + " State: " + p["State"]);
}
}
Console.WriteLine("Returned " + allProjects + " projects in the subscription");

Categories