Using the undocumented SPUtility.SendEmail method. « Blog of an overweight SharePoint addict

1 Comment

As with a lot of the sharePoint API, the SPUtility class and more importantly the sendEMail method lacks a decent amount of documentation on its use.

After a little bit of playing around with a console session, I’ve found what seems to be the best way to use this function. (For me at least.. feel free to disagree!!)

Requires:
using System.Collections.Specialized;

For the purposes of my test code, I used a console sessions and wrapped the send email process inside two using statements to populate site and web with the test sharepoint site.


StringDictionary headers = new StringDictionary();
headers.add("to","reginald@myfatblog.co.uk");
headers.add("cc","thisiscopy@myfatblog.co.uk");
headers.add("bcc","thisistheblindcopy@myfatblog.co.uk");
headers.add("from","MySharePointEmail@myfatblog.co.uk");
headers.add("subject","How to use SendEMail from SPUtility");
headers.add("content-type","text/html"); //This is the default type, so isn't neccessary.

string bodyText =”This is the body of my email, in html format.”;

SPUtility.SendEmail(web, headers, bodyText);

The SendEmail method makes it very easy to pop emails out using your SharePoint sites current SMTP configuration, the only downside is that this does not allow you to use attachments. If you need to do this, then you’ll need to look down the .Net mail methods.. (I found a decent post on David Fekkes blog on how to do this using the .Net methods. I haven’t tried it yet, but it’s a good starting point. Email with attachments from SharePoint code.

A better method to email. Rather than create your own .NET mail object, use the one provided in SharePoint server. Neat !!

Advertisements

C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects. – ASP.NET Forums

2 Comments

The example below show how easy can get different roles groups and users by using SPWeb object. This example is split in thee levels.

 

First Level:

Get all users, groups, roles from SharePoint.

 

Second Level:

When the users exist in you selected SPWeb object, all specific information should me inserted into generic class UserListCollection.

 

Third Level:

Example class below shows the algorithms, how you get the user information from ArrayList object and cast the object into UserListCollection.

 

 

using System;

using System.Collections;

 

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

 

namespace Framework.Business.Components

{ 

            /// <summary>

            /// UsersCollection is object to get all users by selected web object.

            /// </summary> 

            public class UsersCollection

            {

                        private ArrayList    _usersListCollection;   //All incomming users.

                        private SPWeb       _selectedWeb;           //Web position.

                        private string         _groupName;            //Group name.

  

                        private const string _allGroups  = “ALL_GROUPS”;

 

                        /// <summary>

                        /// SPWebLocation property.

                        /// </summary>

                        public SPWeb SPWebLocation

                        {

                                   get{return _selectedWeb;}

                                   set{_selectedWeb = value;}

                        }

 

                        /// <summary>

                        /// SPGroupName property.

                        /// </summary>

                        public string SPGroupName

                        {

                                   get{return _groupName;}

                                   set{_groupName = value;}

                        }

 

                        /// <summary>

                        /// To run the example you need to initialize the UsersCollection object with two instance website and GroupName objects.

                        /// </summary>

                        /// <param name=”webSite”>SPWeb object</param>

                        /// <param name=”groupName”>string group name</param>

                        public UsersCollection(SPWeb webSite ,string groupName)

                        {    

                                   //Check if the SPWeb object is not null.

                                   if(webSite != null)

                                   {

                                               if(groupName == null || groupName.Length == 0)

                                               {     

                                                           this.SPGroupName = _allGroups;

                                               }

                                               _usersListCollection = new ArrayList();

 

                                   }

                                   else

                                   {

                                               Console.WriteLine(“ERROR WEB LOCATION: Your web location have a null value.”);

                                   }

                        }

 

                        /// <summary>

                        /// This public method get all users from SharePoint web location and get user related information.

                        /// </summary>

                        public ArrayList GetUsersByGroup()

                        {

                                   if(this.SPGroupName == _allGroups)

                                   {

                                               foreach(SPGroup singleGroup in this.SPWebLocation.Groups)

                                               {

                                                           UsersList(singleGroup);

                                               }

                                   }

                                   else

                                   {

                                               SPGroup singleGroup = this.SearchGroup(this._groupName);

                                               if(singleGroup != null)

                                               {

                                                           this.UsersList(singleGroup);

                                               }

                                               else

                                               {

                                                           Console.WriteLine(“ERROR WEB GROUP: ” +  this._groupName + ” GROUP NOT EXIST.”);

                                               }

                                   }

                                   return _usersListCollection;

                        }

 

                        /// <summary>

                        /// This private method get users by selected SPGroup object.

                        /// </summary>

                        /// <param name=”group”>SPGroup object</param>

                        private void UsersList(SPGroup group)

                        {

                                   foreach(SPUser singleUser in group.Users)

                                   {

                                               foreach(SPRole singleRole in singleUser.Roles)

                                               {

                                                           _usersListCollection.Add(new UserListCollection(

                                                               singleUser.LoginName,singleRole.Name,group.ParentWeb.Title));

                                               }

                                   }

                        }

 

                        /// <summary>

                        /// This method search for SPGroup name.

                        /// </summary>

                        /// <param name=”group”>string</param>

                        /// <returns>SPGroup object</returns>

                        private SPGroup SearchGroup(string group)

                        {

                                   SPGroup groupObject = null;

                                   foreach(SPGroup singleGroup in this.SPWebLocation.Groups)

                                   {

                                               if(group == singleGroup.Name)

                                               {

                                                           groupObject = singleGroup;

                                               }

                                   }

                                   return groupObject;

                        }

            }

   

            /// <summary>

            /// Generic class UserListCollection

            /// </summary> 

            public class UserListCollection

            {

                        private string _userName = “”;

                        private string _userRole = “”;

                        private string _userWeb = “”;

 

                        /// <summary>

                        /// Generic username.

                        /// </summary>

                        public string UserName

                        {

                                   get { return _userName; }

                                   set { _userName = value; }

                        }

 

 

                        /// <summary>

                        /// Generic user role.

                        /// </summary>

                        public string UserRole

                        {

                                   get { return _userRole; }

                                   set { _userRole = value; }

                        }

 

                        /// <summary>

                        /// Generic SharePoint web name.

                        /// </summary>

                        public string WebName

                        {

                                   get { return _userWeb; }

                                   set { _userWeb = value; }

                        }

 

                        /// <summary>

                        /// This constructor insert three values.

                        /// </summary>

                        /// <param name=”userName”>Name of NT user account.</param>

                        /// <param name=”userGroup”>SharePoint web group name.</param>

                        /// <param name=”userWeb”>SPSite web name.</param>

                        public UserListCollection(string userName, string userGroup, string userWeb)

                        {

                                   this.UserName   = userName;

                                   this.UserRole     = userGroup;

                                   this.WebName   = userWeb;

                        }

            }

 

            /// <summary>

            /// Example to run UsersCollection class.

            /// </summary>

            class Example

            {

                        /// <summary>

                        /// Example to get all users info with generic UsersCollection class.

                        /// </summary>

                        public static void GetUsers()

                        {

                                   //Url location object.

                                   Uri url = new Uri(“http://localserver/&#8221;);

 

                                   SPGlobalAdmin globalAdmin = new SPGlobalAdmin();

                                   SPVirtualServer  virtualServer = globalAdmin.OpenVirtualServer(url);

 

                                   //Get all users by site position and group.

                                   UsersCollection user = new UsersCollection(

                                               virtualServer.Sites[0].AllWebs[0],”Reader”);

 

                                   ArrayList usersArray = user.GetUsersByGroup();

                                   for(int i = 0;i < usersArray.Count;i++)

                                   {

                                               //Generic class

                                               UserListCollection userExample = (UserListCollection)usersArray[i];

                                               Console.WriteLine(“UserName: ” + userExample.UserName);

                                               Console.WriteLine(“Role: ” + userExample.UserRole);

                                               Console.WriteLine(“Web: ” + userExample.WebName);

                                   }

                        }

            }

}

Hallelujah !! A solution baby !!