RSS

Monthly Archives: April 2010

Programmatically insert an item in sharepoint list using ASP.NET user control


  • Create a new web project in Visual studio
  • Add reference to Microsoft.SharePoint.dll  (you can find from C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI)
  • Create an new user control(.ascx)
  • Import below namespaces
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Search;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
  • Insert Textboxes, buttons….whatever u required
  • On button click paste the code below
  • To host the user control in sharepoint use smartpart from codeplex.

CODE SNIPPET

{

            try
            {

               
                //create the object of SPSite
             SPSite oSiteCollection = SPContext.Current.Site;
           

           // SPSite oSiteCollection = new SPSite(“http://mysite:3434“);

                //create the object of SPWeb with the help of SPSite object:
                SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
                oWebsiteRoot.AllowUnsafeUpdates = true;

                //create the connection to the custom List and create object of SPListItemCollection
                SPList oList = oWebsiteRoot.Lists[“MyListName”];
                SPListItemCollection listItemCOll;

                //add the item in the User List
                SPListItem oListItem = oList.Items.Add();
                oListItem[“Name”] = txtName.Text;
                oListItem[“Email”] = txtEmail.Text;
                oListItem[“Color”] = ddldropdown.SelectedItem;
                oListItem[“AssignTo”] = txtAssignto.Text;

                oListItem.Update();             
                            
             
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }

View the UPDATED CODE HERE

 
16 Comments

Posted by on April 21, 2010 in ASP.NET, Code Snippets

 

Tags: ,

How to add numbering column in dataview webpart?


  • Open the page in sharepoint designer
  • Insert dataview wepart
  • Select your columns to query
  • Now click on the first column & insert new column on left
  • In the new column just paste below code…thts it

<xsl:value-of select=”position()” />

If you need any bulleted images. insert an image in the column.

 
1 Comment

Posted by on April 21, 2010 in Code Snippets, Sharepoint Designer