SharePoint Developer Center >
SharePoint Products and Technologies Forums
>
SharePoint - Development and Programming
>
How to get current server name into code ?
How to get current server name into code ?
- 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
- 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.
- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM
- 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.- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM
- 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- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM
All Replies
- 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.- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM
- Hi,
If your code runs on the server, as it is the case for the code of the code behind of the ASP.NET page, like button event handler, you can get the computer name (that is the server name in this case) using one of the following lines:
//it is the DNS name see: http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname.aspx
String hostDNSName = System.Net.Dns.GetHostName();
// it is the NetBIOS name see: http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx
String hostNetBiosName = Environment.MachineName;
But I'm not sure it will solve your issue.
Peter - 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.
- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM
- 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- Marked As Answer byCharlie WuModeratorThursday, November 12, 2009 1:47 AM


