Friday, April 10, 2015

Exception Logging in K2

public class K2ExceptionHandler
    {
            System.Net.Mail.MailMessage expMail = null;
            System.Net.Mail.SmtpClient smtpClient = null;
            System.Net.Mail.MailAddress mailAddressFrom = null;
            Exception ex = null;
            object exContext = null;             
            string emailBody = string.Empty;
            bool bFlag = true;

            public void SendExceptionEmail(SourceCode.KO.ExceptionContext K2)
            {
            try
            {
                smtpClient = new System.Net.Mail.SmtpClient();

                expMail = new System.Net.Mail.MailMessage();

                //Mail To Address
                string mailTO = System.Configuration.ConfigurationManager.AppSettings["ExceptionEmailTo"]; // Get it from K2HostServer.exe.config file, located at "C:\Program Files (x86)\K2 blackpearl\Host Server\Bin"
                expMail.To.Add(mailTO);

                //Email From Address - Get it from String tabble
                string fromAddress = K2.StringTable["From Address"];
                mailAddressFrom = new System.Net.Mail.MailAddress(fromAddress);
                expMail.From = mailAddressFrom;

                //Email Subject
                expMail.Subject = "K2 Error: ProcessID " + K2.ProcessInstance.ID.ToString() + " for the Process, '" + K2.ProcessInstance.Process.FullName + "' is in error state";

                //Send HTML Email
                expMail.IsBodyHtml = true;
                expMail.Priority = System.Net.Mail.MailPriority.High;

                // Basic Message
                emailBody += "";
                emailBody += "";
                emailBody += "";
                emailBody += "";
                emailBody += "K2 Notification";
                emailBody += "
";
                emailBody += "";

                emailBody += ""
;
                emailBody += "
Dear Administrator,
";
                emailBody += "

";

                emailBody += "
"
+ " Error occurred in the K2 workflow, '" + K2.ProcessInstance.Process.FullName + "
' for the Process Intstance ID, '" + K2.ProcessInstance.ID.ToString() + "'. Below is a detailed extraction of the error and the process instance.";
                emailBody += "

";

                #region Process Instance Details

                emailBody += "
Process Intstance Details
";
                              
                emailBody += " " ;
                emailBody += "
"
;
                emailBody += ""
;

                emailBody += " " ;
                emailBody += "
ID:
";
                emailBody += "
"
+ K2.ProcessInstance.ID.ToString() + "
";
                emailBody += "
";

                emailBody += " " ;
                emailBody += "
Process Name:
";
                emailBody += "
"
+ K2.ProcessInstance.Process.FullName + "
";
                emailBody += "
";
               
                emailBody += " " ;
                emailBody += "
Folio:
";
                emailBody += "
"
+ K2.ProcessInstance.Folio + "
";
                emailBody += "
";
              
                emailBody += " " ;
                emailBody += "
Originator:
";
                emailBody += "
"
+ K2.ProcessInstance.Originator.DisplayName + "
";
                emailBody += "
";
                emailBody += " " ;
                emailBody += "
Start Date:
";
                emailBody += "
"
+ K2.ProcessInstance.StartDate.ToString() + "
";
                emailBody += "
";

                emailBody += "
";
                emailBody += "
";
                emailBody += "
";
                emailBody += "

";

                #endregion

                #region Process Data Fields
                emailBody += "
Data Field Values
";
                emailBody += " " ;
                emailBody += "
"
;
                emailBody += ""
;



                foreach (SourceCode.KO.DataField _dataField in K2.ProcessInstance.DataFields)
                {
                    emailBody += " " ;
                    emailBody += "
"
+ _dataField.Name + " [" + _dataField.FieldType.ToString() + "]
";
                    emailBody += "
"
+ _dataField.Value.ToString() + "
";
                    emailBody += "
";


                emailBody += "
";
                emailBody += "
";
                emailBody += "
";
                emailBody += "

";
                #endregion

                #region Failure Details
                emailBody += "
Failure Details
";
                emailBody += " " ;
                emailBody += "
"
;
                emailBody += ""
;

                //Check where the error happened
                switch (K2.ContextType)
                {
                    case SourceCode.KO.ContextType.ClientEvent:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.ClientEventContext)exContext).ActivityInstanceDestination.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Event Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.ClientEventContext)exContext).Event.Name + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.DestinationRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.DestinationRuleContext)exContext).ActivityInstance.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Destinations:
";
                            emailBody += "
"
;
                            foreach (SourceCode.KO.Destination _dest in ((SourceCode.KO.DestinationRuleContext)exContext).Destinations)
                            {
                                emailBody += " " + _dest.Name + " [" + _dest.Type.ToString() + "];";
                            }
                            emailBody += "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Slot Data:
";
                            emailBody += "
"
;
                            foreach (string _slotdata in ((SourceCode.KO.DestinationRuleContext)exContext).SlotInstanceData)
                            {
                                emailBody += " " + _slotdata + ";";
                            }
                            emailBody += "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Dynamic Queues:
";
                            emailBody += "
"
+ ((SourceCode.KO.DestinationRuleContext)exContext).DynamicQueues.ToString() + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Resolve queues to users:
";
                            emailBody += "
"
+ ((SourceCode.KO.DestinationRuleContext)exContext).ResolveQueuesToUsers.ToString() + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Single instance:
";
                            emailBody += "
"
+ ((SourceCode.KO.DestinationRuleContext)exContext).SingleInstance.ToString() + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Number of slots:
";
                            emailBody += "
"
+ ((SourceCode.KO.DestinationRuleContext)exContext).Slots.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.EscalationAction:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EscalationActionContext)exContext).ActivityInstance.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Redirect:
";
                            emailBody += "
"
+ ((SourceCode.KO.EscalationActionContext)exContext).Redirect.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.EscalationRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EscalationRuleContext)exContext).ActivityInstance.Activity.Name + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.EventEscalationAction:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventEscalationActionContext)exContext).EventInstance.ActivityInstanceDestination.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Event Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventEscalationActionContext)exContext).EventInstance.Event.Name + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.EventEscalationRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventEscalationRuleContext)exContext).EventInstance.ActivityInstanceDestination.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Event Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventEscalationRuleContext)exContext).EventInstance.Event.Name + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.EventSucceedingRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventSucceedingRuleContext)exContext).EventInstance.ActivityInstanceDestination.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Event Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventSucceedingRuleContext)exContext).EventInstance.Event.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Succeeding rule:
";
                            emailBody += "
"
+ ((SourceCode.KO.EventSucceedingRuleContext)exContext).SucceedingRule.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.IPCEvent:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.IPCEventContext)exContext).ActivityInstanceDestination.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Event Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.IPCEventContext)exContext).Event.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
IPC process:
";
                            emailBody += "
"
+ ((SourceCode.KO.IPCEventContext)exContext).Process + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
IPC connection string:
";
                            emailBody += "
"
+ ((SourceCode.KO.IPCEventContext)exContext).ConnectionString + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Synchronous:
";
                            emailBody += "
"
+ ((SourceCode.KO.IPCEventContext)exContext).Synchronous + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.LineRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Start activity name:
";
                            emailBody += "
"
+ ((SourceCode.KO.LineRuleContext)exContext).LineInstance.StartActivityInstance.Activity.Name + "
";
                            emailBody += "
";

                            //The folllowing throws error as it does not have finish activity details
                            //_emailBody += " ";
                            //_emailBody += "
Finish activity:
";
                            //_emailBody += "
" + ((SourceCode.KO.LineRuleContext)_exContext).LineInstance.FinishActivityInstance.Activity.Name + "
";
                            //_emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Line name:
";
                            emailBody += "
"
+ ((SourceCode.KO.LineRuleContext)exContext).LineInstance.Line.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Line rule:
";
                            emailBody += "
"
+ ((SourceCode.KO.LineRuleContext)exContext).LineRule.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.PrecedingRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity name:
";
                            emailBody += "
"
+ ((SourceCode.KO.PrecedingRuleContext)exContext).ActivityInstance.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Preceeding rule:
";
                            emailBody += "
"
+ ((SourceCode.KO.PrecedingRuleContext)exContext).PrecedingRule.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.ProcessEscalationAction:
                    case SourceCode.KO.ContextType.ProcessEscalationRule:
                    case SourceCode.KO.ContextType.ProcessFinishRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.ProcessStartRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Message:
";
                            emailBody += "
"
+ ((SourceCode.KO.ProcessStartRuleContext)exContext).Message + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Start rule:
";
                            emailBody += "
"
+ ((SourceCode.KO.ProcessStartRuleContext)exContext).Start.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.ServerEvent:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.ServerEventContext)exContext).ActivityInstanceDestination.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Event Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.ServerEventContext)exContext).Event.Name + "
";
                            emailBody += "
";

                            break;
                        }
                    case SourceCode.KO.ContextType.StartRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.StartRuleContext)exContext).ActivityInstance.Activity.Name + "
";
                            emailBody += "
";
                            break;
                        }
                    case SourceCode.KO.ContextType.SucceedingRule:
                        {
                            emailBody += " " ;
                            emailBody += "
Error thrown by:
";
                            emailBody += "
"
+ K2.ContextType.ToString() + "
";
                            emailBody += "
";

                            exContext = K2.ContextObject;
                            emailBody += " " ;
                            emailBody += "
Activity Name:
";
                            emailBody += "
"
+ ((SourceCode.KO.SucceedingRuleContext)exContext).ActivityInstance.Activity.Name + "
";
                            emailBody += "
";

                            emailBody += " " ;
                            emailBody += "
Succeeding rule:
";
                            emailBody += "
"
+ ((SourceCode.KO.SucceedingRuleContext)exContext).SucceedingRule.ToString() + "
";
                            emailBody += "
";
                            break;
                        }
                    default:
                        {
                            emailBody += "
colspan=\"2\">No details available
";
                            break;
                        }
                }

                emailBody += "
";
                emailBody += "
";
                emailBody += "
";
                emailBody += "

";
                #endregion

                #region Exception Details
                emailBody += "
Exception Details
";
                emailBody += " " ;
                emailBody += "
"
;
                emailBody += ""
;

                ex = new Exception("NOTHING");
                ex = (Exception)K2.ExceptionObject;
                bFlag = true;
                while (bFlag == true)
                {
                    if (ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }
                    else
                    {
                        emailBody += " " ;
                        emailBody += "
Message:
";
                        emailBody += "
"
+ ex.Message + "
";
                        emailBody += "
";

                        emailBody += " " ;
                        emailBody += "
Stack Trace:
";
                        emailBody += "
"
+ ex.StackTrace + "
";
                        emailBody += "
";
                        bFlag = false;
                    }
                }
               
                emailBody += "
";
                emailBody += "
";
                emailBody += "
";
              
                #endregion

                

                #region View Flow link

                string K2ServerName = string.Empty;

                //Workflow Management Server
                string strWorkflowServer = K2.StringTable["Workflow Server"]; //Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=dlx;Port=5252
                string[] workflowServerDetails = strWorkflowServer.Split(';');
                foreach (string strWorkflowServerDetail in workflowServerDetails)
                {
                    if (strWorkflowServerDetail.Contains("Host"))
                    {
                        K2ServerName = strWorkflowServerDetail.Substring(strWorkflowServerDetail.IndexOf('=') + 1);
                    }
                }

                string strViewFlowURL = @"http://###K2Server###:81/ViewFlow/ViewFlow.aspx?ViewTypeName=ProcessView&K2Server=###K2Server###:5252&HostServerName=###K2Server###&HostServerPort=5555&ProcessID=###ProcinstID###";
                strViewFlowURL = strViewFlowURL.Replace("###K2Server###", K2ServerName);
                strViewFlowURL = strViewFlowURL.Replace("###ProcinstID###", K2.ProcessInstance.ID.ToString());

                string strViewFlowLink = "<" + "a href='" + strViewFlowURL + "' target=_blank>Click Here
";
                emailBody += "

";
                emailBody += "
To open the K2 View Flow, "
+ strViewFlowLink + ".
";
                emailBody += "

";
                #endregion


                emailBody += "
Regards,
";
                emailBody += "
K2 Exception Handler
";
                emailBody += "
";
                emailBody += "
";
                emailBody += "
";

                expMail.Body = emailBody;

                #region Send Email

                string smtpMailServer = K2.StringTable["Mail Server"]; //Get mail server from String table

                using (smtpClient as IDisposable)
                {

                    if (smtpMailServer.IndexOf(":") == -1)
                    {
                        smtpClient.Host = smtpMailServer;
                    }
                    else
                    {
                        string[] connection = smtpMailServer.Split(new Char[] { ':' });

                        Int32 I = 0;

                        if (string.IsNullOrEmpty(connection[0]) || string.IsNullOrEmpty(connection[1]))
                        {
                            throw new Exception("Invalid SMTP Server / Port arguments supplied.");
                        }

                        // PORT 2ND PARM ONLY!
                        if (Int32.TryParse(connection[1], out I))
                        {
                            smtpClient.Host = connection[0];
                            smtpClient.Port = Int32.Parse(connection[1]);
                        }
                        else
                        {
                            throw new Exception("Invalid SMTP Server / Port arguments supplied.");
                        }
                    }
                    using (expMail)
                    {
                        smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                        smtpClient.UseDefaultCredentials = true;
                        smtpClient.Send(expMail);                       
                    }
                }
                #endregion
            }
            catch (Exception expMail)
            {
                //Console.WriteLine("[Process Error Notify] Error sending email: " + exMail.Message); //todo Log error to Sharepoint Logs
            }
            finally
            {
                expMail = null;
                smtpClient = null;
                mailAddressFrom = null;
                throw ((Exception)K2.ExceptionObject);
            }
        }

    }

Wednesday, March 25, 2015

K2 Destination Configurations for Work list Items to be available for all destination users even if any destination user already opened the item

Scenario: I want to have a work list item to be visible for all Users in an AD group, even if one of the user already opened the work list item. Also, I need to advance to the next step in workflow when one of the user in Group takes action.
I don’t want the worklist item to be disappeared from the User’s Worklist as soon as one user opens it. All users should be able to see the worklist item until one of the user takes action.
Resolution:
Go to Destination Rule Options of the Activity.
Select Plan per Destination – All at once
Create a slot for each destination.
Resolve all roles and groups to users since I want any user in the role or group can action the item.







In the Destination Sets, Add the user/roles/groups who can action the item.
You can also add multiple users, Roles in the Destination Sets.
Click Next and Finish.








Go to Outcome succeeding rule wizard








Open each configured outcome -
You will see that
All slots of Action Result =







Change it to
Atleast 1 of ActionResult =


Do the same for all other Outcomes.







Saturday, March 21, 2015

Assigning K2 worklist Item to Dynamic Multiple Destination Users - Configuration

Adding Multiple Users for Destination in K2 workflow:


Scenario: Sometimes we may need to assign a work list to a list of multiple users (approvers).  We can achieve this functionality as follows:
  1. Create a Process Field “UsersList” with “;” separated list of userids(along with domain)
EX: denallix\bob;denallix\riverarod;denallix\jarmanj2




Note: make sure there is no semi-colon at the end of the string.


  1. Update the Destination Users as follows.
Make sure you selected Advanced Settings.




i.e. Configure Function as follows:




To Send email to the Users, Configure as follows
For Email Activity Do as Follows:







Open the email Event. And Set “Destination user” as recipient.




Right Click on K2 Designer for Visual Studio 2010 not working

Right click on the kprx file of Visual Studio 2010 Designer for K2 stopped working. Not able to go to Line properties in the workflow.

Solution:


Go to Start => All Programs => Microsoft Visual Studio 2010 =>Visual Studio Tools => 
Right click on “Visual Studio Command Prompt (2010)” and Run as administrator.
Type “devenv /setup”  and hit enter.

cid:image001.png@01CEC98A.838E3D50
If still not working, try below solution:

1.     Ensure Visual Studio 2010 is not running. Check for and shut-down the devenv.exe process if necessary.
2.     Using Windows Explorer, browse to the folder with the originally extracted installation file e.g.:
[your extraction folder]\Setup\Installation
3.     Right-click on the K2 for Visual Studio 2010.msi file and select Repair
http://help.k2.com/helppages/k2blackpearlUserGuide4.6.4/images/TS_VS2010_K2Context%20menu-right-click.png

The K2 Designer extension will be repaired in a few seconds and the mouse right-click context menu and K2 toolbar will be available in Visual Studio 2010 once more.


Wednesday, February 13, 2013

All about SandBox Solutions

SandBox Solutions:
  • Scope of the solution is Site Collection and not entire farm.
  • Deployed and managed by Site Collection Administrators.
  • Sandbox solutions are hosted on SharePoint User Code Server Worker Process(SPUCWorkerProcess.exe) where as Farm solutions are hosted on IISWorkerProcess(w3wp.exe)
  • Sandbox solutions are uploaded to Solution Gallery(_Catalogs/Solutions).
  • No need of IIS reset or IIS App Pool recyclying.
  • Used for Web Parts, Event Receivers, Feature Receivers, Custom SharePoint Designer Workflow Activities.
  • Sandbox solutions are stored in Content Database not in GAC.
  • When a site is restored, it gets Sandbox Solutions also, if any.
  • Assemblies in a sandbox solution are included in Solution package(.wsp) and package is deployed to Site Collection 's Solution Gallery.
  • When Sandbox solution is accessed(i.e when user navigates to a page that contains web part from Sandbox solution), any assemblies in the solution are extracted from Package in the gallery and copied to the file system(C:\ProgramFiles\Microsoft\SharePoint\UCCache) of the server that is handling the sandbox request.
  • For Sandbox solutions coding, referrence the Microsoft.Sharepoint.dll from  the path, ...\14\UserCode\Assemblies. For Form solutions this dll can be  referrenced from ...\14\ISAPI folder.
  • To access resources outside a site collection from within Sandbox solution, you must implement and call a Full Trust Sandbox Proxy.
Disadvantages:
  • Can't access outside Sandbox(Ex: Database, Registry, Environment Variables)
  • Can only access same domain, other domains can't be accessed.
  • Can't be used for application pages, visual web parts, code-based workflows, unmanaged code, web service calls, SPUtility.Sendmail, RunWithElevatedPrevilages, Writing to Disk, Accessing objects in other site collection.
  • Sandbox solutions by default have code limits to 300 points.
  • Microsoft.Sharepoint.WebPartPages not allowed
Powershell Commands:

  • To upload Sandbox solution to Site Collection's Solution gallery, 
          ADD -SPUserSolution -LiteralPath .wsp 
                                               -Site

  • To Deploy(Activate) SandBox Solution,
          Install -SPUserSolution -Identity .wsp
                                                 -Site 
  • To Update  SandBox Solution,
          Update -SPUserSolution -Identity .wsp                                                  -Site 
                                                  -ToSolution .wsp