Showing posts with label Sandbox. Show all posts
Showing posts with label Sandbox. Show all posts

Monday, March 5, 2012

SharePoint Customization

When I tried to build site using sandbox, I found many restrictions. The better solution for most of these problems do a workarounds using out of the box controls and Web parts of the SharePoint.

I found site give us good solutions for building some modules with small customization of out of the box SharePoint Web parts and Controls with css and javascript functions:
http://usermanagedsolutions.com/SharePoint-User-Toolkit/default.aspx
It Contains solution for SharePoint 2007 and SharePoint 2010 Sample of them:
  • Color-Coding-Calendar-List:
    Display calendar with color depend on category of item in List
  • Pie and Bar Charts (Google Connector)
    Display chart using calendar chart depend on values in List
  • Easy-Tabs
    When added to a Web Part zone, the Easy Tabs automatically generate a tabbed interface, with one tab per Web Part present in the zone.
  • Cross-Site-List-Snapshot
    This script allows you to display in a SharePoint page a view taken from another site or site collection.
  • Tasks-Lists-Rollup:
    roll-up Web Part that displays items collected from multiple lists of the same type
  • Bookmarklet-Expand-Collapse-Web-PartsOn a SharePoint page:
    adds an expand/collapse button to each Web Part, next to the title.
  • Page-Redirect:
    This script allows you to redirect your users to another Web page (specified in the Target location), when they try accessing the current page.
  • Image-Rotator:
    The image rotator allows you to display on your page a picture randomly selected from a SharePoint picture library.

    You can use solutions and add more enhancement of it.







Saturday, March 3, 2012

SharePoint 2010 Sandboxed limitation

SharePoint 2010 define a way of packaging called sandbox because old way of packaging is not suitable for shared environment. If there is a problem of package it will effect the environment. But new packaging technique will effect only sandbox's modules was deployed in this site collection.
Components of the sandbox:
  • User Code Service (SPUCHostService.exe). This is responsible for creating the sandbox worker processes that execute individual sandboxed solutions and for allocating requests to these processes.
  • Sandbox Worker Process (SPUCWorkerProcess.exe). This is the process in which any custom code in your sandboxed solution executes.
  • Sandbox Worker Process Proxy (SPUCWorkerProcessProxy.exe). This provides a full-trust environment that hosts the SharePoint API. This enables sandboxed solutions to make calls into the subset of the SharePoint object model that is accessible to sandboxed solutions.

But Sandbox has many limitations:

  1. You can use most of SharePoint API but you can only use current site collection. All you information must be saved in current site.
  2. No Security Elevation (You cannot impersonate). You can only run with current logged in user. You must give a user actual permission to access SharePoint item.
  3. No Email Support (You cannot send email). Only workaround start workflow with activity to send the mail but take care of needed permission to start workflow.
  4. You cannot use SharePoint Web Controls.(You can use asp net controls instead of it).
  5. You cannot do WebRequest connections,(http, web services, wcf, and etc). To access those use through javascript or silverlight.
  6. No GAC Deployment (Sandbox solutions are not stored in File System, they are stored in the database) C:\ProgramData\Microsoft\SharePoint\UCCache temporary for global assembly cache.
  7. Enterprise features (Search, BCS, etc.) are not allowed.
  8. CAS does not allow you to access database. All data storage in SharePoint List.
  9. CAS does not allow you access IO:
    • Global folders like “_layout” & “resources” are not authorized. To use resource file embed it in the dlls.
    • No Visual Webparts
    • No Custom workflow
    • Allow features depend on physical files are not allowed.
  10. No Threading.
  11. No P-Invoke
  12. Sandbox work in separate domain and initialize new page serialize some information to communicate with w3wp.exe.
    Serialized Information which can use in sandbox:
    • ID's of Form, Web Part Manager, Zone, Toolpane
    • Web Part properties
    • Options and properties of the Web Part and zone, such as chrome, zone customization, width, height...
    • View state
    • Control state
    • Server Variables (except APPL_PHYSICAL_PATH and PATH_TRANSLATED)
    • Request Headers
    • Input Stream
    • Current context (List Id, Item Id)
    • Query String

    Serialized Information which can use in sandbox:
    • The Cache object
    • Cookie
    • The ScriptManager or callback
    • The ClientScriptManager
    • The HttpRequest.Files collection (work with limited size to 128KB
    • The Master Page
    • Embedded resources (these are requested by the WebResource.axd and they cannot resolve the Sandboxed assemblies from the W3WP.exe process)
    • You cannot use redirection (Response.Redirect, Server.Transfer or SPUtility.Redirect)
    • You cannot export a Sandboxed Web Part (it will just export the SPUserCodeWebPart)
    • Also you cannot access page object (you cannot control hide or show controls in page like HideCustomAction and CustomActionGroup)
  13. PropertyBags of SharePoint Object model are not accessible
  14. ASP.NET 2.0 web controls are not fully working (those that are using “WebResource.axd” for internal binaries resources, as .axd extension is not allowed). Instead of embed them upload to SharePoint and refer to them as external resources.
  15. Also Sandbox limited number of resources used and deactivate sandbox solution if they reached limited number of resources. limitation depend in number of point limitation by administrator and calculated as this table:


    Metric Name

    Description

    Units

    Resources Per Point

    Hard Limit

    AbnormalProcessTerminationCount

    Process gets abnormally terminated

    Count

    1

    1

    CPUExecutionTime

    CPU exception time

    Seconds

    200

    60

    CriticalExceptionCount

    Critical exception fired

    Number

    10

    3

    InvocationCount

    Number of times solution


    has been invoked

    Count

    N/A

    N/A

    PercentProcessorTime

    Note: # of cores not factored in

    Percentage Units of Overall Processor Consumed

    85

    100

    ProcessCPUCycles



    CPU Cycles

    1E+11

    1E+11

    ProcessHandleCount



    Windows Handles

    10,000

    5,000

    ProcessIOBytes

    (Hard Limit Only) Bytes written


    to IO

    Bytes

    1E+07

    1E+08

    ProcessThreadCount

    Number of Threads


    in Overall Process

    Threads

    10,000

    200

    ProcessVirtualBytes

    (Hard Limit Only)


    Memory consumed

    Bytes

    1E+09

    4E+09

    SharePointDatabaseQueryCount

    SharePoint DB Queries Invoked

    Number

    400

    100

    SharePointDatabaseQueryTime

    Amount of time spent waiting


    for a query to be performed

    Seconds

    20

    60

    UnhandledExceptionCount

    Unhanded Exceptions



    50

    3

    UnresponsiveprocessCount

    We have to kill the process because it has become unresponsive

    Number

    2

    1

    For more details check URL:
    http://msn.microsoft.com/en-us/library/ff798382.aspx


Sunday, January 29, 2012

Capatcha using Javascript

Most of the websites use the technique of captcha for validation / verification purpose whenever someone tries accomplish membership or want to submit a piece of information. The generation of captcha can be done in various ways. We can use server side scripting or even we can use client side scripting.
I am need to host SharePoint 2010 in shared environment and does not allow me to deploy server side control. Then tha best way to use captcha is javascript.

Here is a sample code of capatcha using javascript:

<html>
<head>
<title>Captcha</title>

<script type="text/javascript">

//Created / Generates the captcha function
function DrawCaptcha()
{
var a = Math.ceil(Math.random() * 10)+ '';
var b = Math.ceil(Math.random() * 10)+ '';
var c = Math.ceil(Math.random() * 10)+ '';
var d = Math.ceil(Math.random() * 10)+ '';
var e = Math.ceil(Math.random() * 10)+ '';
var f = Math.ceil(Math.random() * 10)+ '';
var g = Math.ceil(Math.random() * 10)+ '';
var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
document.getElementById("txtCaptcha").value = code
}

// Validate the Entered input aganist the generated security code function
function ValidCaptcha(){
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (str1 == str2) return true;
return false;

}

// Remove the spaces from the entered and generated code
function removeSpaces(string)
{
return string.split(' ').join('');
}


</script>



</head>
<body onload="DrawCaptcha();">
<table>
<tr>
<td>
Welcome To Captcha<br />
</td>
</tr>
<tr>
<td>
<input type="text" id="txtCaptcha"
style="background-image:url(1.jpg); text-align:center; border:none;
font-weight:bold; font-family:Modern" />
<input type="button" id="btnrefresh" value="Refresh" onclick="DrawCaptcha();" />
</td>
</tr>
<tr>
<td>
<input type="text" id="txtInput"/>
</td>
</tr>
<tr>
<td>
<input id="Button1" type="button" value="Check" onclick="alert(ValidCaptcha());"/>
</td>
</tr>
</table>
</body>
</html>

This is simple but you can complicate it to eliminate cracking.

Thursday, January 26, 2012

Use email in SharePoint 2010 Sandbox

In SharePoint 2010 Sandbox. In Sandbox Microsoft.SharePoint.Utilitiesis not avaliable. When tried to use System.Net.Mail methods available to us. SmtpClient constructor give me error.
I found another way using SharePoint Designer workflow. You can trigger a workflow to run programmatically and also supply information to the workflow and set the initiation values. Here's the code to start a workflow:


///
/// Method to handle starting the appropriate Workflow as specified by the Site Collection Workflow Name property
///

private void StartWorkflow()
{
SPWorkflowAssociation wfa = SPContext.Current.Web.WorkflowAssociations.GetAssociationByName(this.SiteCollectionWorkflowName, CultureInfo.InvariantCulture);
wfa.AssociationData = this.GetWorkflowInitiationData();
SPContext.Current.Site.WorkflowManager.StartWorkflow(null, wfa, this.GetWorkflowInitiationData(), SPWorkflowRunOptions.Asynchronous);
}


///
/// Method to construct the Workflow Initiation/Association Data
///

/// A string containing the XML schema and values necessary for the Association Data
private String GetWorkflowInitiationData()
{
String schema = "http://www.w3.org/2001/XMLSchema\" " +
"xmlns:dms=\"http://schemas.microsoft.com/office/2009/documentManagement/types\" " +
"xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" " +
"xmlns:q=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields\" " +
"xmlns:d=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields\" " +
"xmlns:ma=\"http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes\" " +
"xmlns:pc=\"http://schemas.microsoft.com/office/infopath/2007/PartnerControls\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"{0}" +
"
";



StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}", this.SendToEmailAddress);
sb.AppendFormat("{0}", this._subject.Text);
sb.AppendFormat("{0}", this._comments.Text);
sb.AppendFormat("{0}", this._email.Text);
sb.AppendFormat("{0}", this.AutoResponseSubject);
sb.AppendFormat("{0}", this.AutoResponseText);
return String.Format(schema, sb.ToString());
}




Thursday, January 19, 2012

SharePoint 2010 Sandbox Visual WebPart

When you use sandbox Visual Web Part and TemplateControl.LoadControl method is not working in a sandbox solution?
The reason is quite simple – your code in a sandbox solution actually executes in a console application – the SPUCWorkerProcess.exe. It is a console application has created an HttpContext and a Page instance, in which your web part class is also instantiated on that page. You don’t have some crucial asp.net pieces as the HostingEnvironment and the VirtualPathProvider – without these two the LoadControl method simply won’t work. And it is not that you don’t have access to the file system, even if you had, the LoadControl method still wouldn’t work because there wouldn’t be a VirtualPathProvider to provide the file to the asp.net compiler. The bad news is that the same holds for the TemplateControl.ParseControl method too.

Microsoft create template supports Visual Web Parts in the sandbox from Visual Studio 2010 you can use when download Visual Studio 2010 Sharepoint Power Tools