0

I'm trying to populate a dropdownlist with the webapplications in my farm. This dropdownlist is supposed to be visible from the custom property pane on the visual webpart that I'm creating. This is the custom property.

      public string webAppDeploy;
      [WebBrowsable(true), WebDisplayName("Webapplication for deploy"), Description(""),       Personalizable(PersonalizationScope.Shared), Category("My Category"), DefaultValue("Test")]
      public string WebAppDeploy {get { return webAppDeploy; } set { webAppDeploy = value; } } 

I'm guessing that it has to be an enum instead of a string ofcourse but I don't know how to populate the enum with the webapplications.

1

Use this method to get all web from Farm

public DataTable GetAllWebApplication()
{
DataTable dt = new DataTable();
dt.Columns.Add(“URL”);
dt.Columns.Add(“Title”);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local);
foreach (SPWebService webService in webServices)
{
foreach (SPWebApplication wa in webService.WebApplications)
{
dt.Rows.Add(new object[] { wa.GetResponseUri(SPUrlZone.Default).AbsoluteUri, wa.Name });
}
}

});

return dt;
}

Have a look at these for how to implement dynamic dropdown as webpart property.

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/d52e2e1b-5938-43c0-9708-8d9d710880ae/custom-web-part-properties-in-sharepoint-2010-visual-web-partsdynamic-dropdown

http://dev-tips.blogspot.in/2007/06/custom-property-of-webpart-dynamic.html

0

to get your webapp and its url you can do:

    private static void getWebApps()
    {
        SPServiceCollection services = SPFarm.Local.Services;

        foreach (SPService curService in services)
        {
            if (curService is SPWebService)
            {
                SPWebService webService = (SPWebService)curService;

                foreach (SPWebApplication webApp in webService.WebApplications)
                {
                    string a = webApp.Name + " : "+ webApp.GetResponseUri(SPUrlZone.Default).AbsoluteUri);

                }
            }
        }
    }

to add it to the webpart properties follow:

http://msdn.microsoft.com/en-us/library/hh228018.aspx

together youll get what you want! obviously what iv done in code needs to be ammended ;) as im returning a string that would look like this:

mysite - 20133 : http://mysite.welcom

first part is the name of the webapp and the second part is the url! you can change the method to return somthing more usefull like a url like so:

as a global varible control add:

        private DropDownList ddl;

within create child controls add:

        SPServiceCollection services = SPFarm.Local.Services;
        ddl = new DropDownList();

        foreach (SPService curService in services)
        {
            getWebApps(curService);
        }

now call the new method that adds a url control to the dropdownlist:

    private static SPFieldUrlValue getWebApps(SPService curService)
    {

        if (curService is SPWebService)
        {
            SPWebService webService = (SPWebService)curService;
            SPFieldUrlValue spfuv = null;
            foreach (SPWebApplication webApp in webService.WebApplications)
            {
                spfuv = new SPFieldUrlValue();
                spfuv.Description = webApp.Name;
                spfuv.Url = webApp.GetResponseUri(SPUrlZone.Default).AbsoluteUri;

                ddl.Controls.Add(spfuv);
            }

        }
        return null;
    }

now you should have a dropdownlist that contains all the webapplications! you can now add the dropdownlist to the webpart settings as is outlined in the msdn link ;)

  • I'm not sure if it makes any difference but I'm doing this in SharePoint 2013 and I can't find CreateChildControls anywhere. It seems that it has been taken out for 2013. Also thanks for the help I'm trying to get it to work but I can't get the webapplications into the enumerator on the custom property still. I'll keep at it and get back to you. – john_acton Sep 11 '13 at 7:07
  • @john_acton i think it does exist! you are using a webpart? msdn.microsoft.com/en-us/library/… so it must have this class! i presume your doing the code in the ascx codebehind? rather than adding the control to a class and overriding the creatchildcontrols? if so you can do it on onload! – Ali Jafer Sep 11 '13 at 7:48
  • I'm using a Visual webpart, not sure if that makes a difference. – john_acton Sep 11 '13 at 8:02
  • ok thats fine! try and see if you can ovveride as the link iv shown you! otherwise you will need to do it on override of onload but im 100% sure creatchildcontrols exist and will work for you, now follow this dev-tips.blogspot.co.uk/2007/06/… also note the enum doesnt work for dynamic contents as its static (pre defined) what makes it a huge issue! – Ali Jafer Sep 11 '13 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.