Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Last active December 11, 2015 20:39
Show Gist options
  • Save devendrasv/4657073 to your computer and use it in GitHub Desktop.
Save devendrasv/4657073 to your computer and use it in GitHub Desktop.
Complete Solution
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace SharePoint_webpart.Projects
{
[ToolboxItemAttribute(false)]
public class Projects : WebPart
{
//declare all variables
TextBox txtproject;
Button btnadd;
//load all controls
protected override void CreateChildControls()
{
txtproject = new TextBox();
txtproject.ID = "txtproject";
btnadd = new Button();
btnadd.ID = "btnadd";
btnadd.Text = "Add Project";
btnadd.Click += btnadd_Click;
Controls.Add(txtproject);
Controls.Add(btnadd);
}
//Add the project to a list
void btnadd_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
//SPList projectsList = web.Lists["Projects"];
//use the below line code for best prectices
SPList projectsList = web.GetList("http://sharepoint-journey/sites/Apps/Lists/Projects/AllItems.aspx");
SPListItem newItem = projectsList.Items.Add();
newItem["Title"] = txtproject.Text.ToString();
newItem.Update();
txtproject.Text = string.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment