Saturday, April 23, 2011

SharePoint Cartoons

Something that I found hilarious Smile:

image image
image image
image image

Code to Browse the Security Setup of SharePoint Site Collection

In case if you need to verify the security setup of your SharePoint site collection, you just need to run the following code as shown below:

using System;
using System.Linq;
using Microsoft.SharePoint;

namespace BrowseSecurity
{
    class Program
    {
        static void Main(string[] args)
        {
            BrowseSecurity("http://localhost");
            Console.Out.WriteLine(true);
        }

        private static void BrowseSecurity(string url)
        {
            using (SPSite site = new SPSite(url))
            {
                SPWeb web = site.OpenWeb();
                Console.WriteLine("\n\nUsers:");
                foreach (SPUser user in web.Users)
                {
                    Console.WriteLine(user.Name);
                }

                Console.ReadLine();
                Console.WriteLine("\n\n All Users:");
                foreach (SPUser user in web.AllUsers)
                {
                    Console.WriteLine(user.Name);
                }
                Console.ReadLine();
                Console.WriteLine("\n\n Site Users:");
                foreach (SPUser user in web.AllUsers)
                {
                    Console.WriteLine(user.Name);
                }
                Console.ReadLine();
                Console.WriteLine("\n\n Roles:");
                foreach (SPRole role in web.Roles)
                {
                    Console.WriteLine(role.Name);
                }
                Console.ReadLine();
                Console.WriteLine("\n\n Roles Definitions:");
                foreach (SPRoleDefinition roledef in web.RoleDefinitions)
                {
                    Console.WriteLine(roledef.Name);
                }
                Console.ReadLine();
                Console.WriteLine("\n\n Roles Assignments:");
                foreach (SPRoleAssignment roleA in web.RoleAssignments)
                {
                    Console.WriteLine("The following Role definition bindings exist for " +
                    roleA.Member.Name);
                    foreach (SPRoleDefinition roledef in roleA.RoleDefinitionBindings)
                    {
                        Console.WriteLine(roledef.Name);
                    }
                }
                Console.ReadLine();
                Console.WriteLine("\n\n Groups:");
                foreach (SPGroup group in web.Groups)
                {
                    Console.WriteLine(group.Name);
                }
                Console.ReadLine();
            }
        }
    }
}

Sunday, April 17, 2011

Send Email in SharePoint via .NET SmtpClient Class and SharePoint SPUtility Class

In SharePoint, you could send email programmatically by using either .NET Class Library or SharePoint Object Model. I always prefer the second method since using SharePoint ensures that the required settings are maintained by Central Administration.

The following show the two code examples on how you could do this:

Sending Email via .NET SmtpClient Class

using System.Net.Mail;
using Microsoft.SharePoint;

/// <summary>
/// Sends the mail via NET SmtpClient.
/// </summary>
/// <param name="Subject">The subject.</param>
/// <param name="Body">The body.</param>
/// <param name="IsBodyHtml">if set to <c>true</c> [is body HTML].</param>
/// <param name="From">From.</param>
/// <param name="To">To.</param>
/// <param name="Cc">The cc.</param>
/// <param name="Bcc">The BCC.</param>
/// <returns></returns>
public static bool SendMailviaNET(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
{
    bool mailSent = false;
    try
    {
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
        MailMessage mailMessage = new MailMessage(From, To, Subject, Body);

        if (!String.IsNullOrEmpty(Cc))
        {
            MailAddress CCAddress = new MailAddress(Cc);
            mailMessage.CC.Add(CCAddress);
        }
        if (!String.IsNullOrEmpty(Bcc))
        {
            MailAddress BCCAddress = new MailAddress(Bcc);
            mailMessage.Bcc.Add(BCCAddress);
        }

        mailMessage.IsBodyHtml = IsBodyHtml;
        smtpClient.Send(mailMessage);
        mailSent = true;
    }
    catch (Exception)
    {
        return mailSent;
    }
    return mailSent;
}

Sending Email via SharePoint SPUtility Class

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;

/// <summary>
/// Sends the mail via SharePoint SPUtility.
/// </summary>
/// <param name="Subject">The subject.</param>
/// <param name="Body">The body.</param>
/// <param name="IsBodyHtml">if set to <c>true</c> [is body HTML].</param>
/// <param name="From">From.</param>
/// <param name="To">To.</param>
/// <param name="Cc">The cc.</param>
/// <param name="Bcc">The BCC.</param>
/// <returns></returns>
public static bool SendMailviaSharePoint(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
{
    bool mailSent = false;
    try
    {
        SPWeb thisWeb = SPContext.Current.Web;
               
        StringDictionary headers = new StringDictionary();
        headers.Add("to", To);
        headers.Add("cc", Cc);
        headers.Add("bcc", Bcc);
        headers.Add("from", From);
        headers.Add("subject", Subject);
        if (IsBodyHtml) headers.Add("content-type", "text/html");

        mailSent = SPUtility.SendEmail(thisWeb, headers, Body);
    }
    catch (Exception)
    {
        return mailSent;
    }
    return mailSent;
}

Saturday, April 16, 2011

SPMalaya Google Search (SPMalaya.CSEWP) Web Part (Licensed-Free)

SharePoint Malaya (SPMalaya) Google Search Web Part (SPMalaya.CSEWP) for SharePoint 2010 allows you to harness the power of Google Custom Search Engine (CSE) to create a customized search experience for your SharePoint sites.

SPMalaya Google Search comprises of four (4) Web Parts:

  • SPMalaya Google Search Box and Results Web Part displays a search box that allows users to search for information via Google Custom Search Engine (CSE). Search results are displayed on the same page (i.e. search element) that provides the most layout and customization options.
  • SPMalaya Google Search Box Web Part displays a search box that allows users to search for information via Google Custom Search Engine (CSE). Search results can be displayed on either Google-hosted page, SharePoint Default page or any SharePoint Custom page.
  • SPMalaya Google Search Results Web Part displays the search results and the properties associated with Google Custom Search Engine (CSE). You will need to configure hosting page option in the Google Search Box Web Part in order for this web part to work.
  • SPMalaya Content Best Bet Web Part displays high-confidence best bet related to the content of your page.

 

image

For more information about SPMalaya Google Search functionalities:

1.1 About the License

SPMalaya Google Search is licensed free of charge.

BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.

For more information about End User License Agreement (EULA), see Microsoft Public License (Ms-PL): http://www.opensource.org/licenses/ms-pl

1.2 Installer

The SPMalaya Google Search installer can be downloaded from here: SPMalaya Google Search Web part Installer v1.0.0 (SharePoint 2010)

1.3 Documentation

The SPMalaya Google Search Installation and Configuration Guide is included with your distribution package. It is also downloadable at: SPMalaya Google Search Installation and Configuration Guide v1.0.0 (SharePoint 2010)

Online version is also available at: http://sharepointmalaya.blogspot.com

1.4 Getting Supports

Ladies and Gents,

I created this product as part of my hobbies and certainly I am not working full time - I work on WEEKENDS ONLY (I wish my kids went to bed at 9pm each night on weekends)!

So, if you have any support issues, feel free to put your comments on this page: http://sharepointmalaya.blogspot.com. If I unable to come back to you in a timely fashion, then I am sure you will get help from the communities ;).

1.5 Feature Requests

I am very happy and pleased for any and all user feedback, and value your opinions in this regard. Please tell me!

Again, feel free to put your comments on this page: http://sharepointmalaya.blogspot.com. Alternatively, you can email me at sharepointmalaya@gmail.com

Kind regards,

Mohd Sukri,

Kuala Lumpur (Malaysia), about me: http://about.me/mohdsukri