Friday, July 20, 2012

Programmatically create List during an initial addition using SharePoint 2010 Visual Web Part

The following code can create the list when the web part is inially added to the web part zone:






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;
using Microsoft.SharePoint.Utilities;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;

namespace USRDC.WebParts.DocAuthorOrganizer
{
    [ToolboxItemAttribute(false)]
    public class DocAuthorOrganizer : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/USRDC.WebParts/DocAuthorOrganizer/DocAuthorOrganizerUserControl.ascx";
       
        private String primaryAuthorList = "Primary Author";
        [WebBrowsable(true),
        WebDisplayName("List Name"),
        WebDescription(""),
        Personalizable(PersonalizationScope.Shared),
        Category("Settings"),
        DefaultValue("Primary Author")
        ]
        public string PrimaryAuthorList
        {
            get
            {
                return primaryAuthorList;
            }
            set
            {
                primaryAuthorList = value;
            }
        }
        private int rowLimitPerPage = 2000;
        [WebBrowsable(true),
        WebDisplayName("Row Limit"),
        WebDescription(""),
        Personalizable(PersonalizationScope.Shared),
        Category("Settings"),
        DefaultValue(1000)
        ]
        public int RowLimitPerPage
        {
            get
            {
                return rowLimitPerPage;
            }
            set
            {
                rowLimitPerPage = value;
            }
        }
        private string sharePointGroup = "OperationalReports";
        [WebBrowsable(true),
        WebDisplayName("SharePoint Group Name"),
        WebDescription(""),
        Personalizable(PersonalizationScope.Shared),
        Category("Settings"),
        DefaultValue(1000)
        ]
        public string SharePointGroup
        {
            get
            {
                return sharePointGroup;
            }
            set
            {
                sharePointGroup = value;
            }
        }
        public bool isconfig;
        private Button btnSave;
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            if (!Utilities.ListExists(web, primaryAuthorList))
            {
                // Create a list
                Guid listId = web.Lists.Add(primaryAuthorList, "A list for storing a primary author for document library", SPListTemplateType.GenericList);
                SPList newList = web.Lists[listId];
                newList.OnQuickLaunch = false;
                newList.ContentTypesEnabled = true;
                newList.EnableFolderCreation = false;
                newList.Update();
            
                SPListItem item = newList.Items.Add();
                item["Title"] = "Young Ryu";
                item.Update();
               
                // Set DefaultView with AllItems
                SPView listView = newList.DefaultView;
                listView.Update();
            }
            isconfig = false;
        }
        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
            WebPartManager wp = WebPartManager.GetCurrentWebPartManager(Page);
            if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)
            {
                btnSave = new Button();
                btnSave.ID = "btnSave";
                btnSave.Text = "Submit";
                Panel pnl1 = new Panel();
                LiteralControl ltc = new LiteralControl("<Div class='ConfigWebPartTitle'>The following SharePoint List must be created:</Div>");
                pnl1.Controls.Add(ltc);
                ltc = new LiteralControl("<Div class='ConfigWebPartText'>Lists: " + primaryAuthorList + "</Div>");
                pnl1.Controls.Add(ltc);
                pnl1.Controls.Add(btnSave);
                btnSave.Click += new EventHandler(BtnSave_Click);
                this.Controls.Add(pnl1);
                isconfig = true;
                if (!Page.IsPostBack)
                {
                    if (Utilities.ValidateCollateral(SPContext.Current.Web, "Item", primaryAuthorList))
                    {
                        this.Controls.Remove(pnl1);
                        isconfig = false;                       
                    }
                }
                else
                {
                    if (Utilities.ValidateCollateral(SPContext.Current.Web, "Item", primaryAuthorList))
                    {
                        this.Controls.Remove(pnl1);
                        isconfig = false;                       
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment