Tuesday, October 15, 2013

Connect application service to a web application programmatically

The following code will connect a custom user profile application service to a web application.
(SharePoint 2010)


           SPSite site = new SPSite("http://current site url");

           //Get all Service Proxies in the farm
           SPServiceProxyCollection farmServices = SPFarm.Local.ServiceProxies;

            // Iterate all the proxies
            foreach (SPServiceProxy service in farmServices)
            {
                SPServiceApplicationProxyCollection serviceApplicationColl = service.ApplicationProxies;
                foreach (SPServiceApplicationProxy serviceApp in serviceApplicationColl.OfType<SPServiceApplicationProxy>())
                {
                    if (serviceApp.DisplayName == "My Custom User Profile" && serviceApp.TypeName.ToLower().Contains("user profile service"))
                    {
                        //Add the service proxy to the web application
                        site.WebApplication.ServiceApplicationProxyGroup.Add(serviceApp);
                       
                         // update the application proxy group
                        site.WebApplication.ServiceApplicationProxyGroup.Update();

                         //update the web application
                        site.WebApplication.Update();
                 
                    }
                }
            }


No comments:

Post a Comment