none
End point configuration in web.config file programatically RRS feed

  • Question

  • Hi,

    I want to add an entry for Biztalk endpoint into web.config file programatically into section :

      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
      </system.serviceModel>

    which is multiline section

    <bindings>
                <wsHttpBinding>
                    <binding name="WSHttpBinding_ITwoWayAsyncVoid" />
                </wsHttpBinding>
            </bindings>
            <client>
                <endpoint address="url"
                    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITwoWayAsyncVoid"
                    contract="name"
                    name="WSHttpBinding_ITwoWayAsyncVoid">
                    <identity>
                        <userPrincipalName value="" />
                    </identity>
                </endpoint>
            </client>

    Unable to add it. I am able to add for single line..in <assembly> 

    Tried with <system.web> section also with following code, but still didn't worked, gave me error :

    public class Feature1EventReceiver : SPFeatureReceiver
        {
             public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                RemoveAllCustomisations(webApp);
    
                #region Enable session state
    
                SPWebConfigModification httpRuntimeModification = new SPWebConfigModification();
                httpRuntimeModification.Path = "configuration/system.web/pages";
                httpRuntimeModification.Name = "enableSessionState";
                httpRuntimeModification.Sequence = 0;
                httpRuntimeModification.Owner = "WebConfigModifications";
                httpRuntimeModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
                httpRuntimeModification.Value = "true";
                webApp.WebConfigModifications.Add(httpRuntimeModification);
    
                            #endregion
    
                /*Call Update and ApplyWebConfigModifications to save changes*/
                webApp.Update();
                webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }

    Request you to help me out as soon as possible.

    Thanking you in advance.

    Regards,

    Jayashri

    Thursday, March 16, 2017 10:26 AM

Answers

  • Thanks for the updates.

    All above mentioned did not work. So thought to do it in another way as follows:

    1. Added "Service Reference" of the specified Biztalk ur named it as "BizSerReference"

    2. Code as follows:

    SPList list = web.Lists["Framework Contract Overview"]; SPListItem newItem = properties.ListItem; BizSerReference.FrameworkContract FWC = new BizSerReference.FrameworkContract(); var fieldComment = list.Fields.GetFieldByInternalName("Comment"); var fieldDescription = list.Fields.GetFieldByInternalName("Description"); var fieldCountry = list.Fields.GetFieldByInternalName("Country"); var fieldFWCResponsible = list.Fields.GetFieldByInternalName("FWCResponsible"); var fieldPBU = list.Fields.GetFieldByInternalName("PBU"); if (newItem["Status"].ToString() != "Draft") { FWC.FrameworkId = newItem.ID.ToString(); //ID if (newItem["Comment"] != null && !string.IsNullOrEmpty(newItem["Comment"].ToString())) { FWC.Comment = fieldComment.GetFieldValueAsText(newItem["Comment"]); //Comment }

    } FWC.Description = fieldDescription.GetFieldValueAsText(newItem["Description"]); WSHttpBinding binding = new WSHttpBinding(); EndpointAddress address = new EndpointAddress("url"); BizSerReference.FrameworkContractServiceClient client = new BizSerReference.FrameworkContractServiceClient(binding, address); client.Upsert(FWC);

    Thanks for all your help.

    Regards,

    Jayashri

    • Marked as answer by Jayashri10 Monday, May 15, 2017 4:58 AM
    Monday, May 15, 2017 4:58 AM

All replies

  • Hi,

    You could try this.

    SPWebConfigModification entry = new SPWebConfigModification("system.serviceModel", "configuration");               
                    entry.Sequence = 0;
                    entry.Owner = "feature";
                    entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                    entry.Value = @"<system.serviceModel>
                            <serviceHostingEnvironment aspNetCompatibilityEnabled=""true"" />    
                            <bindings>
                              <wsHttpBinding>        
                                <binding name=""WSHttpBinding_IAuthenticationService"">
                                  <security mode=""TransportWithMessageCredential"">
                                    <transport clientCredentialType=""None"" />
                                    <message clientCredentialType=""UserName"" establishSecurityContext=""false"" />
                                  </security>
                                </binding>
                              </wsHttpBinding>
                            </bindings>
                            <client>      
                            <endpoint address=""Url"" binding=""wsHttpBinding"" bindingConfiguration=""WSHttpBinding_IAuthenticationService""
                            contract=""testService.IMyService"" name=""WSHttpBinding_IAuthenticationService"" />
                            </client>
                                </system.serviceModel>";
                    webApp.WebConfigModifications.Clear();                
                    webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();
                    webApp.WebConfigModifications.Add(entry);
                    service.ApplyWebConfigModifications();
                    webApp.Update();  

    Here is the referenced thread.

    http://sharepoint.stackexchange.com/questions/145632/remove-bindings-using-spwebconfigmodification

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 17, 2017 12:52 PM
  • Hello,

    Thanks for your reply.

    It's not working for me. 

    :(

    Is there any way using powershell?

    Thanking you in advance.

    Regards,

    Jayashri

    Monday, March 20, 2017 2:59 AM
  • Hi,

    You could try with PowerShell but it should be same way.(I have tested above code and it’s work actually)

    You could check below link for PowerShell script.

    https://social.technet.microsoft.com/Forums/office/en-US/afdd1409-c504-4347-9921-5dd6311fc989/updating-webconfig-entry-in-multiple-front-end-servers?forum=sharepointadmin

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 21, 2017 10:38 AM
  • Thanks for reply. 

    Shall check about powershell.

    If possible, could you please share all code, which worked for you through visual studio, I mean whole feature activated and deactivated event receiver, so I can cross check, where I am doing mistake or why it is not working for me.

    Thanking you in advance.

    Regards,

    Jayashri

    Friday, March 24, 2017 2:18 AM
  • Hi,

    You don't require to replicate all the features of Lee's to run the code successfully. Could you please post your error description here we can point you the code line where you can correct.

    If there is any error while you running this code, it will break web.config for your webapplication and site cannot be accessible.

    You can verify the correct xml string that constructed for proper opening and closing end/.


    Please remember to click Mark as Answer on the answer if it helps you

    Friday, March 24, 2017 3:29 AM
  • I am not getting any error but neither getting "web.config" updated.

    Please check my below code again, 

     public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                SPWebApplication webApp = web.Site.WebApplication;
                
                SPWebConfigModification entry = new SPWebConfigModification("system.serviceModel", "configuration");
    
                entry.Sequence = 0;
                entry.Owner = "feature";
                entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                entry.Value = @"<system.serviceModel>
                            <serviceHostingEnvironment aspNetCompatibilityEnabled=""true"" />    
                            <bindings>
                              <wsHttpBinding>        
                                <binding name=""WSHttpBinding_IAuthenticationService"">
                                  <security mode=""TransportWithMessageCredential"">
                                    <transport clientCredentialType=""None"" />
                                    <message clientCredentialType=""UserName"" establishSecurityContext=""false"" />
                                  </security>
                                </binding>
                              </wsHttpBinding>
                            </bindings>
                            <client>      
                            <endpoint address=""http://services.t01d.grp.com/FrameworkContractService.svc"" binding=""wsHttpBinding"" bindingConfiguration=""WSHttpBinding_IAuthenticationService""
                            contract=""BiztalkServiceReference.FrameworkContractService"" name=""WSHttpBinding_IAuthenticationService"" >
                            <identity>
                             <userPrincipalName value=""Dev@.global.network"" />
                            </ identity >
                            </endpoint>    
                                </client>
                                </system.serviceModel>";
    
                webApp.WebConfigModifications.Clear();
                webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();
                webApp.WebConfigModifications.Add(entry);
                webApp.WebService.ApplyWebConfigModifications();
                webApp.Update();
    
            }

    Code Deployed Successfully.

    Friday, March 24, 2017 6:23 AM
  • Hi,

    Here is my test code for your reference.

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //properties.Feature.Parent as SPSite based on your feature scope, here i set as site
                    using (SPSite site = properties.Feature.Parent as SPSite)
                    {
                        SPWebApplication webApp =site.WebApplication;
                        SPWebService service = webApp.WebService;
                        SPWebConfigModification entry = new SPWebConfigModification("system.serviceModel", "configuration");
                        entry.Sequence = 0;
                        entry.Owner = "feature";
                        entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                        entry.Value = @"<system.serviceModel>
                            <serviceHostingEnvironment aspNetCompatibilityEnabled=""true"" />    
                            <bindings>
                              <wsHttpBinding>        
                                <binding name=""WSHttpBinding_IAuthenticationService"">
                                  <security mode=""TransportWithMessageCredential"">
                                    <transport clientCredentialType=""None"" />
                                    <message clientCredentialType=""UserName"" establishSecurityContext=""false"" />
                                  </security>
                                </binding>
                              </wsHttpBinding>
                            </bindings>
                            <client>      
                            <endpoint address=""Url"" binding=""wsHttpBinding"" bindingConfiguration=""WSHttpBinding_IAuthenticationService""
                            contract=""testService.IMyService"" name=""WSHttpBinding_IAuthenticationService"" />
                            </client>
                                </system.serviceModel>";
                        webApp.WebConfigModifications.Clear();
                        webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();
                        webApp.WebConfigModifications.Add(entry);
                        service.ApplyWebConfigModifications();
                        webApp.Update();
    
                    }
                });
            }

    The configuration section is added to the end.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 24, 2017 7:29 AM
  • Thanks for updates.

    Done changes accordingly but still not working. :(

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0" />
       </system.serviceModel>

    Above entry is already exist in my webapplication's web.config.

    Should I remove those lines?

    What should be feature deployment procedure?

    I am deploying it in GAC, deafult activation is True.

    Please let me know, so can check more in detail.

    Regards,

    Jayashri


    Friday, March 24, 2017 8:17 AM
  • Hi,

    The existing configuration will be replace based on my test, could you try to debug the code?

    Here is one link for your reference.

    https://msdn.microsoft.com/en-us/library/ff798479.aspx?f=255&MSPPError=-2147217396  

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 24, 2017 8:55 AM
  • Hi,

    now when tried debugging code...

    it stopped at line : service.ApplyWebConfigModifications(); webapp.update();

    and thrown error:

    Exception thrown: 'System.ArgumentException' in Microsoft.SharePoint.dll
    Exception thrown: 'System.Web.HttpException' in System.Web.dll

    and sharepoint site page error is :

    An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated. 

    Please help.



    • Edited by Jayashri10 Saturday, March 25, 2017 12:18 PM
    Friday, March 24, 2017 1:12 PM
  • Hi,

    The error message is too general, try to check the error details.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 27, 2017 8:31 AM
  • Hi,

    Please find below details when tried to run above code through console application:

    

    Please help.

    Thanking you in advance.

    Regards,

    Jayashri

    Monday, March 27, 2017 3:58 PM
  • When tried to debug with X64, able to proceed little bit and then got below error:

    Monday, March 27, 2017 4:20 PM
  • Hi,

    Have you call clear to clear the bad entries? Here is one thread mentioned same error for your reference.

    webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();

    http://sharepointcustomization.blogspot.sg/2010/09/spwebconfigmodifications-throws.html

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 29, 2017 9:42 AM
  • Thanks for reply.

    Now error is of "Access Denied" while on line...webapp.update()

    Please find detail and assist.

    Tuesday, April 4, 2017 2:54 PM
  • Hi,

    What’s the user you used? Try to use farm administrator account and check. 

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, April 5, 2017 1:37 AM
  • Thanks for reply.

    I am using Farm administrator account.

    If I deploy code and try to activate(just activate) feature through powershell it works well, web.config gets updated.

     Get-SPFeature  $DisplayName | Enable-SPFeature -Url  $SiteUrl

    But it didn't work well with deactivation, means even though try deactivate through powershell..it works well with deactivation but entry in web.config, dont disappear, it still in web config.

    Disable-SPFeature $DisplayName -Url $SiteUrl  –Confirm:$false

    and if try with just clicking on deactivate button it gives an error...

    What can be the solution...

    Wednesday, April 5, 2017 4:06 AM
  • Hi,

    So you want to revert the web.config back when deactivate feature?

    If so, you could try to update the web.config back.

    Fake code:

         //when feature deactive
                    SPWebConfigModification entry = new SPWebConfigModification("system.serviceModel", "configuration");               
                    entry.Sequence = 0;
                    entry.Owner = "feature";
                    entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                    entry.Value = @"<system.serviceModel>
                            <serviceHostingEnvironment aspNetCompatibilityEnabled=""true"" />                            
                                </system.serviceModel>";
                    webApp.WebConfigModifications.Clear();                
                    webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();
                    webApp.WebConfigModifications.Add(entry);
                    service.ApplyWebConfigModifications();
                    webApp.Update(); 

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, April 5, 2017 5:29 AM
  • No Success.

    Tried above code, but still its not working...

    Wednesday, April 5, 2017 6:28 AM
  • Hi,

    Try this:

     public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //properties.Feature.Parent as SPSite based on your feature scope, here i set as site
                    using (SPSite site = properties.Feature.Parent as SPSite)
                    {
                        SPWebApplication webApp =site.WebApplication;
                        SPWebService service = webApp.WebService;               
    
                        SPWebConfigModification entry = new SPWebConfigModification("system.serviceModel", "configuration");
                        entry.Sequence = 0;
                        entry.Owner = "Leefeature";
                        entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                        entry.Value = @"<system.serviceModel>
                            <serviceHostingEnvironment aspNetCompatibilityEnabled=""true"" />    
                            <bindings>
                              <wsHttpBinding>        
                                <binding name=""WSHttpBinding_IAuthenticationService"">
                                  <security mode=""TransportWithMessageCredential"">
                                    <transport clientCredentialType=""None"" />
                                    <message clientCredentialType=""UserName"" establishSecurityContext=""false"" />
                                  </security>
                                </binding>
                              </wsHttpBinding>
                            </bindings>
                            <client>      
                            <endpoint address=""Url"" binding=""wsHttpBinding"" bindingConfiguration=""WSHttpBinding_IAuthenticationService""
                            contract=""testService.IMyService"" name=""WSHttpBinding_IAuthenticationService"" />
                            </client>
                                </system.serviceModel>";
                        webApp.WebConfigModifications.Clear();
                        webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();
                        webApp.WebConfigModifications.Add(entry);
                        service.ApplyWebConfigModifications();
                        webApp.Update();
    
                    }
                });
            }
    
    
            // Uncomment the method below to handle the event raised before a feature is deactivated.
    
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //properties.Feature.Parent as SPSite based on your feature scope, here i set as site
                    using (SPSite site = properties.Feature.Parent as SPSite)
                    {
                        SPWebApplication webApp = site.WebApplication;
                        SPWebService service = webApp.WebService;                    
    
                        SPWebConfigModification entry = new SPWebConfigModification("system.serviceModel", "configuration");
                        entry.Sequence = 0;
                        entry.Owner = "Leefeature";
                        entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                        entry.Value = @"<system.serviceModel>
                            <serviceHostingEnvironment aspNetCompatibilityEnabled=""true"" />                            
                                </system.serviceModel>";
                        webApp.WebConfigModifications.Clear();
                        webApp.Farm.Services.GetValue<SPWebService>().WebConfigModifications.Clear();
                        webApp.WebConfigModifications.Add(entry);
                        service.ApplyWebConfigModifications();
                        webApp.Update();
                                     
                    }
                });
            }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, April 7, 2017 8:50 AM
  • Thanks for the updates.

    All above mentioned did not work. So thought to do it in another way as follows:

    1. Added "Service Reference" of the specified Biztalk ur named it as "BizSerReference"

    2. Code as follows:

    SPList list = web.Lists["Framework Contract Overview"]; SPListItem newItem = properties.ListItem; BizSerReference.FrameworkContract FWC = new BizSerReference.FrameworkContract(); var fieldComment = list.Fields.GetFieldByInternalName("Comment"); var fieldDescription = list.Fields.GetFieldByInternalName("Description"); var fieldCountry = list.Fields.GetFieldByInternalName("Country"); var fieldFWCResponsible = list.Fields.GetFieldByInternalName("FWCResponsible"); var fieldPBU = list.Fields.GetFieldByInternalName("PBU"); if (newItem["Status"].ToString() != "Draft") { FWC.FrameworkId = newItem.ID.ToString(); //ID if (newItem["Comment"] != null && !string.IsNullOrEmpty(newItem["Comment"].ToString())) { FWC.Comment = fieldComment.GetFieldValueAsText(newItem["Comment"]); //Comment }

    } FWC.Description = fieldDescription.GetFieldValueAsText(newItem["Description"]); WSHttpBinding binding = new WSHttpBinding(); EndpointAddress address = new EndpointAddress("url"); BizSerReference.FrameworkContractServiceClient client = new BizSerReference.FrameworkContractServiceClient(binding, address); client.Upsert(FWC);

    Thanks for all your help.

    Regards,

    Jayashri

    • Marked as answer by Jayashri10 Monday, May 15, 2017 4:58 AM
    Monday, May 15, 2017 4:58 AM