Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! i Have textboxes that i want to update , to an splist, dont know were to start can somehelp me?
Posted

1 solution

This code takes txt box texts and saves them into SP list. It creates a new list item thurhg "AddANewSPListItem" method, this is custom method but sharpoint services offer built in methods, if an item allready exists then you need to fetch the item and the rest is the same as below. You just need to change the method from create to get.

I dont know however if this is the best way but it works form me :P

C#
 SPListItem spItem = AddANewSPListItem("ListName");
 spItem["FieldName"] = textBox1.Text.Trim();
 spItem["FieldName2"] = textBox1.Text.Trim();

 //update the item
 using (SPSite site = new SPSite(URLOfYourSharepointSite))
 {
    site.WebApplication.FormDigestSettings.Enabled = false;
    site.AllowUnsafeUpdates = true;
    using (SPWeb currWeb = site.OpenWeb())
    {
      currWeb.AllowUnsafeUpdates = true;
      spItem.Update();
      currWeb.AllowUnsafeUpdates = false;
      site.AllowUnsafeUpdates = false;
      site.WebApplication.FormDigestSettings.Enabled = true;
    }
}


Good luck!

B
 
Share this answer
 
Comments
Kurac1 30-Nov-12 8:00am    
SPWeb web = SPContext.Current.Web;

SPList myList = web.Lists["MyList"];
SPListItem spItem = myList.Items.Add();


spItem["Title"] = TextBox_Name.Text.Trim();
spItem["ProductNumber"] = TextBox_ProdNum.Text.Trim();

//update the item
using (SPSite site = new SPSite("http://wingtip"))
{
site.WebApplication.FormDigestSettings.Enabled = false;
site.AllowUnsafeUpdates = true;
using (SPWeb currWeb = site.OpenWeb())
{
currWeb.AllowUnsafeUpdates = true;
spItem.Update();
currWeb.AllowUnsafeUpdates = false;
site.AllowUnsafeUpdates = false;
site.WebApplication.FormDigestSettings.Enabled = true;

I have write like this , dont work!
pykos 30-Nov-12 8:13am    
errors? use a try catch block an see what happens, I cant help you with "it doesnt work" :). I assume "http://wingtip" is the whole site collection, is your list on a sub site like "http://wingtip/subsite" I think this is the problem. Try the url first and then report what happened.

BTW kurac1? Baljkakan? :)

B
Kurac1 30-Nov-12 9:11am    
Hi!
The problem is not that! I have already created an webpart that works but then i have an other webpart there i can edit, the list , but that does not work i can not use try and catch on it beacause when sending data from one webpart to another i am not using connectable webparts , i am using like this

if (ListBox.SelectedItem != null)
{
VisualWebPartSearchLocalProducts.VisualWebPartSearchLocalProductsUserControl myControl=(VisualWebPartSearchLocalProductsUserControl)
Page.LoadControl(@"~/_CONTROLTEMPLATES/SharePointProject/VisualWebPartSearchLocalProducts/VisualWebPartSearchLocalProductsUserControl.ascx");

myControl.SetName(ListBox.SelectedValue);
Controls.Add(myControl);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900