Assign a Mobile Message Service to a Web Application
Applies to: SharePoint Foundation 2010
This topic describes how you can programmatically assign a mobile messaging service account to a Web application.
Creating an MSP Account for a Web Application
Each SharePoint Foundation Web application, including the Central Administration Web application, can have an account with a mobile messaging service provider (MSP). Normally, these accounts are set by a farm administrator in the Application Management portion of the Central Administration application. However, you can programmatically create an account and assign it to a Web application. First, call a SPMobileMessagingAccount() constructor that does not require an SPMobileMessageServiceProvider parameter. Then call the UpdateSmsAccount(SPMobileMessagingAccount) method to assign the account to a Web application. In the following snippet, webApp is a reference to an SPWebApplication object. (See Getting References to Sites, Web Applications, and Other Key Objects for information on how to get a reference to such an object.) The snippet assumes that the file has using statements for Microsoft.SharePoint and System.Security Namespace. Note that the URL of the service must begin with "https"; that is, it must be a Secure Socket Layer (SSL) URL. Note also that the call to UpdateSmsAccount(SPMobileMessagingAccount) is passed as part of an anonymous delegate to the RunWithElevatedPrivileges(SPSecurity.CodeToRunElevated) method.
SecureString password = new SecureString();
password.AppendChar('a');
password.AppendChar('Z');
password.AppendChar('3');
password.AppendChar('&');
SPMobileMessagingAccount account =
new SPMobileMessagingAccount("Contoso Messaging",
"https://contoso.com/services/mobilemessages",
"BobG",
password);
SPSecurity.RunWithElevatedPrivileges(delegate() {webApp.UpdateSmsAccount(account);
webApp.Update();});
Dim password As New SecureString()
password.AppendChar("a")
password.AppendChar("Z")
password.AppendChar("3")
password.AppendChar("&")
Dim account As New SPMobileMessagingAccount("Contoso Messaging", "https://contoso.com/services/mobilemessages", "BobG", password)
SPSecurity.RunWithElevatedPrivileges(Sub()
webApp.UpdateSmsAccount(account)
webApp.Update()
End Sub)
Once the Web application has been assigned an account with the MSP, your code can thereafter use the OutboundSmsServiceAccount property as a reference to the Web application’s account object.