Ask a questionAsk a question
 

AnswerHow to get current server name into code ?

  • Wednesday, November 04, 2009 7:56 PMGtecCR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Folks,

    I have the following issue. We have 2 servers in our PROD Environment: ServerA and ServerB. Both are accessed through a LoadBalancer, so I have the following URL http://mywebapplication.company.com/ and the Load Balancer send me to ServerA XOR ServerB. In my code, I have a Button that opens an specific Attachament that belongs to an SPListItem. When I try to open the attachment (from my own code), the URL that is created is something like this:
    "http://mywebapplication.company.com/Lists/myListName/Attachments/1/myExcelFile.xlsx". When trying to open the file I got the following error message: Unable to connect to the remote server. If I use ServerA instead of http://mywebapplication.company.com, the URL is as follows:
    "http://ServerA/Lists/myListName/Attachments/1/myExcelFile.xlsx" and that way it works. So this is my question: Is there a way to get the current server name (the one that I was redirected by the load balancer) so I can use ServerA xor ServerB instead?

    I really appreciate your help.
    Kind Regards
    • Edited byMike Walsh MVPMVP, ModeratorThursday, November 05, 2009 5:01 AMTitle re-written (Was "SharePoint Environment Issue" which is non-specific)
    •  

Answers

  • Friday, November 06, 2009 8:50 AMJevgeni Borozna Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    For SharePoint there is SPServer.Local, which is an instance of Current server as SPServer object. For example there is properties like DisplayName, Name and others.
  • Friday, November 06, 2009 1:58 AMCharlie WuModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I don't think there is a method to retrieve the current server name into code.

    However, since you are using load balance, I suggest you also use a load balance technique in dealing with this problem.

    You can generate a random number within a range and compare it with it's half. If larger, go to ServerA. Else, go to ServerB.

    The mathematical expectation of this is 50%, which in actually load balanced.
    Keep It Simple and Stupid.
  • Friday, November 06, 2009 1:44 PMPeter Holpar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Additionally, if you have a reference for an SPSite object, you can get the name of the host using its HostName property (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.hostname.aspx).

    Peter

All Replies