Programmatically adding a service call to SharePoint Call Center application - c#

I am working with sharepoint and I am trying to add a service call to microsoft's call center application template. I can add one with just a name with the following code:
SPSite allSites = new SPSite(siteURL);
SPWeb site = allSites.AllWebs[siteName];
SPListItemCollection requestsList = site.Lists[serviceRequests].Items;
SPListItemCollection customerList = site.Lists[customers].Items;
SPListItem item = requestsList.Add();
item["Service Request"] = "Program Test";
//item["Customer"] = "Donald Duck";
item["Customer"] = customerList[0];
item.Update();
First I tried just using the customer name, which did not work. I then got the customer list and tried to use the customer list item instead, but I still get the same error:
"Invalid data has been used to update the list item. The field you are trying to update may be read only."
Does anyone have experience with adding information to sharepoint from code similar to this? Is there someway I can determine which fields are read-only, if any?
Thanks!

I found the solution to this problem, please check out this link.

This sometime happens when you have a Lookup Field and you don't specify the value for it. For Example when you have a List with the Following item
Customer : Text
Department : Lookup of Department List and Required.
SPListItem item = requestsList.Add();
item["Customer"] = "as";
item.Update();
Now you will get this error.Because you didnt specify the value for the Department field

Related

TFS 2018 API: Can access workitems but not workitem API end point

I created a middleware app that will pull work item data from TFS.
I was able to do this using the workitems end point.
http://sampleserver:8080/tfs/sampleproject/_apis/wit/wiql?api-version=4.0/workitems?ids=1,2,3
Now, I also need to get the work item links per work item. Per docu I would need to access the workitem with expand items. But unfortunately, work item end point does not seem to work.
http://sampleserver:8080/tfs/sampleproject/_apis/wit/wiql?api-version=4.0/workitem/3
Am I missing something here?
According to your description, looks like you just want the URL of created WorkItem, so that anyone when click on URL, created Work Item will be Open.
https://tfsurl:8080/tfs/DefaultCollection/PatrickProject/_workitems/edit/172/
The URL should be above format and here DefaultCollection is the collection name and the PatrickProject is the project name. I used this url and got rid of the id '172' in this case and use the ID of newly created work item. This would return the URL to go to the work item HTML page.
So it's a fixed format, if you have Newly Created WorkItem ID and collection name , project name, you just need to follow above format and change the last value of work item ID. That's it , ignore of which work item type you created.
If you want do this with code, do not use Rest API, you need to use client API, sample snippet:
var tfsURI = new Uri("http://test:8080/tfs");
var networkCredential1 = new NetworkCredential("test", "test!");
ICredentials credential = (ICredentials)networkCredential1;
Microsoft.VisualStudio.Services.Common.WindowsCredential winCred = new Microsoft.VisualStudio.Services.Common.WindowsCredential(credential);
VssCredentials vssCredentials = new VssCredentials(winCred);
using (TfsTeamProjectCollection collection = new TfsTeamProjectCollection(tfsURI, vssCredentials))
{
collection.EnsureAuthenticated();
TswaClientHyperlinkService hyperlinkService =
collection.GetService<TswaClientHyperlinkService>();
String TFSurl = hyperlinkService.GetWorkItemEditorUrl(17648).ToString(); //17648 WorkItem ID
}
Hope this Helps!
To get specific work item information you need to use the Get Workitem API call so try http://sampleserver:8080/tfs/sampleproject/_apis/wit/workitems/3?api-version=4.0 instead.
You can also use this http://sampleserver:8080/tfs/sampleproject/_apis/wit/workitems/3?$expand=Links&api-version=4.0 and this will return the work with Id of 3 and all it's links (parent, attached files, changetset, etc.)
Notice that the api-version=4.0 with change depending on the version of TFS/Service you are using and should always be the last string in the REST call.

Add multiple users to SharePoint Online document library item "Person or Group" column using Client Side Object Model (CSOM)

I am attempting to add multiple users to a field using CSOM. However, only one person is being added even though both have been validated.
var newUserField = new List<SP.FieldUserValue>();
newUserField.Add(new FieldUserValue { LookupId = 14 });
newUserField.Add(new FieldUserValue { LookupId = 11 });
newListItem["MultiUserField"] = newUserField;
newListItem.Update();
ctx.ExecuteQuery();
That code always only adds user 14. I have also tried:
newListItem["MultiUserField"] = newUserField.ToArray();
Every reference I have seen says this should work, and it's not throwing any errors.
I have also verified the column allows multiple entries, and have added both users manually as a test. Could this be a document library CSOM limitation? Most references I have seen are using a list.
It turns out, during validating my users, I was calling a
ctx.ExecuteQuery();
that was wiping out what I had saved for the user. Validating the users first and storing their IDs in a list and then updating the item worked.

Retrieve list item using CSOM on SharePoint online isn't working

m developing an app model on SharePoint online using the provider hosted model.
From clientwebpart, I want to access a Sharepoint list item. I can access list a object but can't get the list item (always empty). I already followed the sample code at "Apps for SharePoint sample pack - SharePoint 2013 Perform basic data access operations by using CSOM in apps", but still does not work.
Here is my code:
SharePointContextToken contextToken;
Uri sharepointUrl;
string accessToken;
TokenHelper.TrustAllCertificates();
string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);
if (contextTokenString != null)
{
contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;
using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken))
{
Web web = clientContext.Web;
ListCollection lists = web.Lists;
List selectedList = lists.GetByTitle("LeaveCategory");
clientContext.Load<ListCollection>(lists); // this lists object is loaded successfully
clientContext.Load<List>(selectedList); // this list object is loaded successfully
clientContext.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = #"<View><Query><Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where></Query><ViewFields><FieldRef Name='ID' /></ViewFields></View>";
Microsoft.SharePoint.Client.ListItemCollection listItems = selectedList.GetItems(camlQuery);
clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(listItems); // problem here, this list items is return empty
clientContext.ExecuteQuery();
}
}
Is there any trivial mistake that I am making?
Also, I am trying to create fresh new project and follow instruction of How to: Create a basic provider-hosted app for SharePoint, and appending code toretrieve list item, but it still returns 0 item.
Did anyone ever succeeded achieving this?
Its working now,
its because we must add specific permission for the app at file AppManifest.xml
I add "List" at scope and give it permission "FullControl"
After doing only below along with List scope full permissions, its worked for me:
In the Scope cell, choose Web from the drop down list.
In the Permission cell, choose Read from the drop down list.

Add items to existing SharePoint lists by coding

I want to add items to a still existing SharePoint list by self-coding.
So I searched the internet and found a lot of information how to create lists, add items and so on..
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
SPListItem item = listItems.Add();
The coding seems easy, but where i have to put the code?
I programmed Web Parts and deployed these to my web application.
This was no problem, but here i am missing an approach.
I am using VisualStudion 2008 and SharePoint WSS 3.0
Thank you for any help.
SPWeb app = SPContext.Current.Web;
SPList ListName = app.Lists["YourListName"];
SPListItem ListItem = ListName.Items.Add();
ListItem["field1Name"] = value;
ListItem["field2Name"] = value;
ListItem.Update();
It depends on the purpose - why are you doing this?
Do you have one specific list where you (as the owner/administrator) need to create items and this has to be done only once? If yes, then you can do it with Powershell, as in this Karine Bosch's post.
If you need your users to be able to add items to list using program code, then web parts is the way to go. Then you will want to build a nice user interface for that webpart. And then you can deploy your webpart to your server, create a page in your site and add the webpart to the page.
If you need to create the new list item as soon as something else happens (for instance, a list item is created in some other list, then think of event handlers.
you are doing some modification on site ...so after that we need to update the site ..Use ListItem.update();

How to create web part to insert data in custom list

I'm quite new to sharepoint and what I want to develop is this:
I have a number of custom lists that are connetted between them like a relational DB. What I want to do is to develop a web part that will be able to insert data in all these different lists.
Can someone please give me some hints to where to start from? If there are some tutorials or how tos. Thanks a lot.
AB
There is a nice article about adding items to custom sharepoint list you can check it !
Goes like
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = SPContext.Current.Web;
SPList myList = myWeb.Lists["Custom List"];
SPListItem myListItem = myList.Items.Add();
myListItem["Title"] = oTextTitle.Text.ToString();
myListItem["Employee Name"] = oTextName.Text.ToString();
myListItem["Designation"] = oTextDesignation.Text.ToString();

Categories