Return to Snippet

Revision: 41147
at February 13, 2011 04:03 by petrdamborsky


Updated Code
protected void Page_Load(object sender, EventArgs e)
{
    // http://www.example.com/page?nid=2
    string nid = HttpContext.Current.Request.QueryString["nid"];
    
    if(!string.IsNullOrEmpty(nid))
    {
        using (SPSite site = SPContext.Current.Site)
        {
            using (SPWeb web = site.OpenWeb())
            {
                // needed for list item update
                web.AllowUnsafeUpdates = true;
                // get list
                SPList list = web.Lists["ListA"];
                // get list item by Id from url
                SPListItem item = list.GetItemById(Convert.ToInt32(nid));
                // create new list item
                SPListItem itemCopy = list.Items.Add();
                // copy attributes
                itemCopy["Title"] = item.Title;
                // update db
                itemCopy.Update();
                // set back to default
                web.AllowUnsafeUpdates = false;
            }
        }
    }
}

Revision: 41146
at February 13, 2011 03:58 by petrdamborsky


Initial Code
protected void Page_Load(object sender, EventArgs e)
{
    string nid = HttpContext.Current.Request.QueryString["nid"];
    if(!string.IsNullOrEmpty(nid))
    {
        using (SPSite site = SPContext.Current.Site)
        {
            using (SPWeb web = site.OpenWeb())
            {
                // needed for list item update
                web.AllowUnsafeUpdates = true;
                // get list
                SPList list = web.Lists["ListA"];
                // get list item by Id from url
                SPListItem item = list.GetItemById(Convert.ToInt32(nid));
                // create new list item
                SPListItem itemCopy = list.Items.Add();
                // copy attributes
                itemCopy["Title"] = item.Title;
                // update db
                itemCopy.Update();
                // set back to default
                web.AllowUnsafeUpdates = false;
            }
        }
    }
}

Initial URL

                                

Initial Description

                                

Initial Title
Duplicate list item in code behind (simple)

Initial Tags
sharepoint

Initial Language
C#