Deploying UserControls folder via Feature

Apr 28, 2008 at 7:32 PM
Is there a way to create the UserControls folder and copy the user controls into the folder via the FeatureActivated event of a Feature? HttpContext.Current is null in a feature receiver so I don't know of another way to get a handle on those paths.

Thanks!
Jul 28, 2008 at 1:54 AM
have you figured this out? I created a feature but i cannot save the control the the right path.
Jul 28, 2008 at 6:54 AM
Yeah, I figured it out, but I ended up ripping SmartPart out of my application to make it work. It's actually very easy and more flexible than using SmartPart. I'm still using UserControls, but I'm creating real webparts that dynamically load in the usercontrol, similarly to what SmartPart does. All of my webpart classes inherit from this class..

using System;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;



namespace CSI.ESS
{


    public abstract class ESSWebPartBase<T> : System.Web.UI.WebControls.WebParts.WebPart where T : UserControl
    {

        public ESSWebPartBase()
        {
        }

        private UserControl usercontrol;


        protected override void CreateChildControls()
        {

            base.CreateChildControls();
            this.Controls.Clear();
            var name = GetASCXName();
            var pathToASCX = string.Format(@"/_Layouts/CSI.ESS/{0}.ascx", name);
            usercontrol = (UserControl)Page.LoadControl(pathToASCX);

            T typeSafeControl = (T)usercontrol;
            ConfigureUserControl(typeSafeControl);

            this.Controls.Add(usercontrol);




        }



        protected abstract string GetASCXName();

        protected abstract void ConfigureUserControl(T userControl);

    }
}

With this technique, your webparts can also take advantage of the built-in property management system ("Edit Shared WebPart" thingy).

Sorry if this doesn't make much sense. It's late and I can't sleep. I'll explain more tomorrow if I need to.
Jul 28, 2008 at 5:06 PM
Thanks alex,

if you can, please show the code of the webpart , where you implement this class.
Aug 6, 2008 at 12:42 AM
Sorry about the delay here. I'm a little behind on my emails. :) Anyway, here it is. It's really simple and works like a charm. This particular user control has a few properties that appear in the webpart properties window in SharePoint. I'd take this technique any day over SmartPart (and believe me, I tried the SmartPart route)

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using System.Xml.Serialization;


namespace CSI.ESS
{
    [DefaultProperty("ShowBenefitForm"),
        ToolboxData("<{0}:AvailableFormsWebPart runat=server></{0}:AvailableFormsWebPart>"),
        XmlRoot(Namespace = "CSI.ESS")]
    public class AvailableFormsWebPart : ESSWebPartBase<AvailableForms>
    {



        private bool _ShowBenefitsInquiry = true;
        private bool _ShowCheckInquiry = true;
        private bool _ShowDeductionsInquiry = true;
        private bool _ShowDemographicInquiry = true;
        private bool _ShowDemographicChangeRequest = true;
        private bool _ShowDirectDepositRequest = true;
        private bool _ShowLeaveBalanceInquiry = true;
        private bool _ShowW2Summarization = true;
        private bool _ShowW4Inquiry = true;
        private bool _ShowW4ChangeRequest = true;
        private bool _ShowLeaveRequest = true;




        protected override string GetASCXName()
        {
            return "AvailableForms/AvailableForms";
        }



        [Category("Available Forms"),
            DefaultValue(true),
            Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            WebDisplayName("Show Benefits Inquiry"),
            WebDescription("Check to Show Benefits Inquiry")]
        public bool ShowBenefitsInquiry
        {
            get { return _ShowBenefitsInquiry; }
            set { _ShowBenefitsInquiry = value; }
        }



        [Category("Available Forms"),
            DefaultValue(true),
            Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            WebDisplayName("Show Check Inquiry"),
            WebDescription("Check to Show Check Inquiry")]
        public bool ShowCheckInquiry
        {
            get { return _ShowCheckInquiry; }
            set { _ShowCheckInquiry = value; }
        }


        [Category("Available Forms"), DefaultValue(true), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Show Leave Request"), WebDescription("Check to Show Leave Request")]
        public bool ShowLeaveRequest
        {
            get { return _ShowLeaveRequest; }
            set { _ShowLeaveRequest = value; }
        }

        [Category("Available Forms"),
           DefaultValue(true),
           Personalizable(PersonalizationScope.Shared),
           WebBrowsable(true),
           WebDisplayName("Show Deductions Inquiry"),
           WebDescription("Check to Show Deductions Inquiry")]
        public bool ShowDeductionsInquiry
        {
            get { return _ShowDeductionsInquiry; }
            set { _ShowDeductionsInquiry = value; }
        }

        [Category("Available Forms"),
             DefaultValue(true),
             Personalizable(PersonalizationScope.Shared),
             WebBrowsable(true),
             WebDisplayName("Show Demographic Inquiry"),
             WebDescription("Check to Show Demographic Inquiry")]
        public bool ShowDemographicInquiry
        {
            get { return _ShowDemographicInquiry; }
            set { _ShowDemographicInquiry = value; }
        }

        [Category("Available Forms"),
         DefaultValue(true),
         Personalizable(PersonalizationScope.Shared),
         WebBrowsable(true),
         WebDisplayName("Show Demographic Request"),
         WebDescription("Check to Show Demographic Request")]
        public bool ShowDemographicChangeRequest
        {
            get { return _ShowDemographicChangeRequest; }
            set { _ShowDemographicChangeRequest = value; }
        }

        [Category("Available Forms"),
          DefaultValue(true),
          Personalizable(PersonalizationScope.Shared),
          WebBrowsable(true),
          WebDisplayName("Show Direct Request"),
          WebDescription("Check to Show Direct Deposit Request")]
        public bool ShowDirectDepositRequest
        {
            get { return _ShowDirectDepositRequest; }
            set { _ShowDirectDepositRequest = value; }
        }

        [Category("Available Forms"),
            DefaultValue(true),
            Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            WebDisplayName("Show Leave Balance Inquiry"),
            WebDescription("Check to Show Leave Balance Inquiry")]
        public bool ShowLeaveBalanceInquiry
        {
            get { return _ShowLeaveBalanceInquiry; }
            set { _ShowLeaveBalanceInquiry = value; }
        }

        [Category("Available Forms"),
           DefaultValue(true),
           Personalizable(PersonalizationScope.Shared),
           WebBrowsable(true),
           WebDisplayName("Show W2 Summarization"),
           WebDescription("Check to Show W2 Summarization")]
        public bool ShowW2Summarization
        {
            get { return _ShowW2Summarization; }
            set { _ShowW2Summarization = value; }
        }

        [Category("Available Forms"),
           DefaultValue(true),
           Personalizable(PersonalizationScope.Shared),
           WebBrowsable(true),
           WebDisplayName("Show W4 Inquiry"),
           WebDescription("Check to Show W4 Inquiry")]
        public bool ShowW4Inquiry
        {
            get { return _ShowW4Inquiry; }
            set { _ShowW4Inquiry = value; }
        }

        [Category("Available Forms"),
            DefaultValue(true),
            Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            WebDisplayName("Show W4 Change Request"),
            WebDescription("Check to Show W4 Change Request")]
        public bool ShowW4ChangeRequest
        {
            get { return _ShowW4ChangeRequest; }
            set { _ShowW4ChangeRequest = value; }
        }

        protected override void ConfigureUserControl(AvailableForms userControl)
        {
            userControl.ShowBenefitsInquiry = ShowBenefitsInquiry; //need to set to a webpart property in this class.
            userControl.ShowCheckInquiry = ShowCheckInquiry;
            userControl.ShowDeductionsInquiry = ShowDeductionsInquiry;
            userControl.ShowDemographicInquiry = ShowDemographicInquiry;
            userControl.ShowDemographicChangeRequest = ShowDemographicChangeRequest;
            userControl.ShowDirectDepositRequest = ShowDirectDepositRequest;
            userControl.ShowLeaveBalanceInquiry = ShowLeaveBalanceInquiry;
            userControl.ShowW2Summarization = ShowW2Summarization;
            userControl.ShowW4Inquiry = ShowW4Inquiry;
            userControl.ShowW4ChangeRequest = ShowW4ChangeRequest;
            userControl.ShowLeaveRequest = ShowLeaveRequest;

        }
    }
}