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();
Related
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.
We create a discussion board in Sharepoint 2010 without much effort, but I am looking for a solution where discussion board and all its replies are added to the certain zone automatically. That is end user do not need to add discussion board, through the process of entering page into edit mode and then adding web part. But instead, once user create a discussion board, all discussions and message are added to the page automatically (to certain zone in page).
Thanks.
Add this code the a web event handler (ListAdded) in order to run this code automatically. In that code you will need to check if the list type is of Discussion. In the event receiver you will need get a reference to the SPWeb in which you want to add the web part, and to the list you want to use (in this example the Contacts list). Next you create an instance of the ListViewWebPart class, in which you can set the ZoneID, the ListName and the ViewGuid. This is the tricky part, the ListName property should contain the ID of your list (a GUID), not the name of your list!! But the ListName property is of the type string, so you need to convert the List GUID to a string using .ToString(“B”).ToUpper(). The same goes for the ViewGuid. Finally you need to get a reference to the WebPartCollection for the page in which you want to add the web part (in this example the home page, being default.aspx). Now you can add the web part using the Add method.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
// Get a reference to a web and a list
SPSite site = new SPSite("http://localhost:8000");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Contacts"];
// Instantiate the web part
ListViewWebPart wp = new ListViewWebPart();
wp.ZoneID = "Left";
wp.ListName = list.ID.ToString("B").ToUpper();
wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
// Get the web part collection
SPWebPartCollection coll =
web.GetWebPartCollection("default.aspx",
Storage.Shared);
// Add the web part
coll.Add(wp);
I'm using Microsoft.Exchange.WebServices to work with some public folders and their items.
I would like to create a View item link to a specific item (Microsoft.Exchange.WebServices.Data.Item), but I can't figure out how to get it's path.
something like:
var theLink =
string.Format(#"Click here", item.XXXX);
Any suggestions?
It seems to not be possible anymore to due security.
Instead I created a link to Outlook Web Access instead.
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();
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