Pages

Dec 30, 2010

10 reasons to use Sandboxed Solutions

  • Sandboxed solutions are secure.
  • Sandboxed solutions can be monitored.
  • Sandboxed solutions do not affect other sandboxed solutions, well atleast not in other site collections is what I mean.
  • Sandboxed solutions do not touch the file system for the most part
  • Sandboxed solutions skip application pool recycles, so debugging is not such a pain.
  • Sandboxed solutions allow the site collection administrator to perform deployment and upgrades
  • Sandboxed solutions make CAS policies look like the out of style hairstyles of 1980s
  • The Solution validation framework for sandboxed solutions is exntensible, simply inherit from the SPSolutionValidator base class.
  • Sandboxed solutions remove the need for line by line code reviews
  • Sandboxed solutions allow you to offer different level of SLAs to different site collections using Resource Quotas.

What is supported by sandboxed solutions?

Basically it is a narrowed down subset of the SharePoint OM. This is controlled with VS2010 by selecting your Visual Studio project as a sandboxed solution. It will hide any methods/classes that are not available automatically.

The following capabilities and elements are available in sandboxed solutions:

  • List definitions
  • List instances
  • Onet.xml
  • WebTemplate Feature element instead of Webtemp.xml
  • Content Types/Fields
  • Navigation
  • Module/files
  • Feature callouts
  • Web Parts derived from WebPart
  • Event receivers
    • SPItemEventReceiver
    • SPListEventReceiver
    • SPWebEventReceiver
  • Custom Actions
  • Workflows

What is not supported by sandboxed solutions?

The following capabilities and elements are not available in sandboxed solutions (from MSDN):

  • Custom Action groups
  • HideCustomAction element
  • Content Type Binding
  • Web Application-scoped Features
  • Farm-scoped Features
  • CustomPropertyToolPart Class
  • programmatic workflow
  • Event receivers
    • SPLimitedWebPartManager
  • timer jobs
  • visual WebParts
  • SharePoint mapped folders (e.g. "_layouts", and "images")

The following .NET namespaces are not supported by sandboxed solutions:

  • ADO.NET
  • System.IO
  • System.Security.Cryptography
  • System.Web.UI.ClientScriptManager

Dec 28, 2010

SharePoint 2010 versus SharePoint 2007 Object Model (Microsoft.SharePoint.dll)

There are the differences between the new object model (Microsoft.SharePoint.dll) and the old.  Some classes/interfaces/enums were removed, but nothing that looks to hurt anyone.  The really cool part though...no properties or methods were removed between the two versions (doesn't mean that they are not depreciated however)!  NOTE:This is based on a beta version of SharePoint 2010.
  • Classes/Interfaces/Enums in 2007:   3225
  • Classes/Interfaces/Enums in 2010:   6658
  • New namespaces in 2010:    46
    • Microsoft.BusinessData
      Microsoft.SharePoint.Administration.AccessControl
      Microsoft.SharePoint.Administration.Claims
      Microsoft.SharePoint.ApplicationPages.Calendar
      Microsoft.SharePoint.ApplicationPages.PickerQuery
      Microsoft.SharePoint.Applications.GroupBoard
      Microsoft.SharePoint.Applications.GroupBoard.MobileControls
      Microsoft.SharePoint.Applications.GroupBoard.Utilities
      Microsoft.SharePoint.Applications.GroupBoard.WebControls
      Microsoft.SharePoint.Applications.GroupBoard.WebPartPages
      Microsoft.SharePoint.BusinessData
      Microsoft.SharePoint.BusinessData.Administration
      Microsoft.SharePoint.BusinessData.Infrastructure
      Microsoft.SharePoint.BusinessData.Infrastructure.Collections
      Microsoft.SharePoint.BusinessData.MetadataModel
      Microsoft.SharePoint.BusinessData.MetadataModel.Collections
      Microsoft.SharePoint.BusinessData.MetadataModel.Constants
      Microsoft.SharePoint.BusinessData.MetadataModel.Dynamic
      Microsoft.SharePoint.BusinessData.MetadataModel.Static
      Microsoft.SharePoint.BusinessData.MetadataModel.Static.DataAccess
      Microsoft.SharePoint.BusinessData.Offlining
      Microsoft.SharePoint.BusinessData.Parser
      Microsoft.SharePoint.BusinessData.Runtime
      Microsoft.SharePoint.BusinessData.SharedService
      Microsoft.SharePoint.BusinessData.SharedService.Structs.ExtensionMethods
      Microsoft.SharePoint.BusinessData.SystemSpecific
      Microsoft.SharePoint.BusinessData.SystemSpecific.Db
      Microsoft.SharePoint.BusinessData.SystemSpecific.DotNetAssembly
      Microsoft.SharePoint.BusinessData.SystemSpecific.Wcf
      Microsoft.SharePoint.BusinessData.SystemSpecific.WebService
      Microsoft.SharePoint.BusinessData.Upgrade
      Microsoft.SharePoint.Calculation
      Microsoft.SharePoint.Client
      Microsoft.SharePoint.CoordinatedStreamBuffer
      Microsoft.SharePoint.Diagnostics.ULSEventTemplates
      Microsoft.SharePoint.JSGrid
      Microsoft.SharePoint.JsonUtilities
      Microsoft.SharePoint.MobileMessage
      Microsoft.SharePoint.RBSWrapper
      Microsoft.SharePoint.TenantAdministration
      Microsoft.SharePoint.UserCode
      Microsoft.SharePoint.Utilities.Cab
      Microsoft.SharePoint.Utilities.SimpleParsers
      Microsoft.SharePoint.Utilities.SqlTrace
      Microsoft.SharePoint.Utilities.Win32
      Microsoft.Xslt
  • I have attached lists of
    • All new classes/interfaces
    • All removed classes/interfaces
    • All Property Changes (adds and deletions) to old classes
    • All Methods Changes (adds and deletions) to old classes
Links:
Enjoy!
Pravyn.

HOW TO: Write an SQL Webpart


Start up Visual Studio and create a new webpart. It'll be called webpart1 by default, lets leave it at that for now. First off, right click the project in the solution explorer, and click add reference. Scroll down and select System.Data. Once you've added this we're ready to begin.
Now, make your code look like this:
using System;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace WebPart1
{
    [Guid("493c926d-f885-4965-b72d-ee18e0292630")]
    public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private string _sqlStatement = "SELECT * from tblTest";
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("SQL Statement"),
        WebDescription("Query to return a set of data")]
        public string SQLstatement
        {
            get { return _sqlStatement; }
            set { _sqlStatement = value; }
        }
        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(SQLstatement)
|| !SQLstatement.ToUpper().TrimStart().StartsWith("SELECT")
|| SQLstatement.Contains(";"))
            {
                Literal lit = new Literal();
                lit.Text = "Only single SELECT statement allowed"; Controls.Add(lit); return;
            }
            DataGrid grid = new DataGrid();
            
            //Attempt connection
            try
            {
                
                using (SqlConnection conn = new SqlConnection("server=SP-TEST\\databasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
                {
                    SqlCommand cmd = new SqlCommand(SQLstatement, conn); 
                    conn.Open(); 
                    SqlDataReader reader = cmd.ExecuteReader(); 
                    grid.DataSource = reader; 
                    grid.AutoGenerateColumns = true; 
                    grid.DataBind(); 
                    reader.Close(); 
                    conn.Close();
                } Controls.Add(grid);
            }
            catch (Exception exp)
            {
                Literal errMessage = new Literal();
                errMessage.Text = exp.ToString();
                Controls.Add(errMessage);
            }
        }
    }
}
Now just build the control and your webpart should be working well. Lets take a look at some of the key parts.
private string _sqlStatement = "SELECT * from tblTest";
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("SQL Statement"),
        WebDescription("Query to return a set of data")]
        public string SQLstatement
        {
            get { return _sqlStatement; }
            set { _sqlStatement = value; }
        }
This part sets the SQL query you're about to run.
using (SqlConnection conn = new SqlConnection("server=SP-TEST\\databasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
                {
                    SqlCommand cmd = new SqlCommand(SQLstatement, conn); 
                    conn.Open(); 
                    SqlDataReader reader = cmd.ExecuteReader(); 
                    grid.DataSource = reader; 
                    grid.AutoGenerateColumns = true; 
                    grid.DataBind(); 
                    reader.Close(); 
                    conn.Close();
                } Controls.Add(grid);
This part connects to the database and displays the results in a data grid. Note the server name SP-TEST\\databasearea has two backslashes, this is to counter the escape effect of a backslash.
So that's it really, tiny bit of code with big implications. Hope it opens some doors for you.