Thursday, December 22, 2011

Sandbox Solutions in SharePoint 2010 – Part 1

Before starting what sandbox solution is, first let us understand what solution mean? If you have worked with MOSS 2007 version then you must be aware of solutions. Solutions can be deployed over a farm. With 2010 version solution can also be deployed in sandbox. Solution may contain web parts, images, pages, event handlers, lists etc.


Sandbox solution is a new concept introduced in SharePoint 2010. Advantage of sandbox solution is this solution actually executes in a restricted zone. It cannot access everything in object model. It cannot access certain resources from SharePoint object model.

This is the advantage because it cannot affect the farm environment. If something goes wrong in the sandbox solution then entire farm does not get affected.

Sandbox solution is always given certain number of resource allocation space and that is monitored by farm administrator. If farm administrator wants, then it can be disabled or even it can also be promoted to be a farm solution as an upgrade.

Other advantage of sandbox solution is it can free farm administrator for deployment stand point. Site collection administrator can deploy and activate the sandbox solution. If the sandbox solution does not contain any assembly, then even a user with full control over a top level site of site collection can also deploy the solution.

Sandbox solution runs in a separate process and that is the reason if something goes wrong in sandbox solution then it does not impact farm. It does not run in w3wc process but runs under SPUCWorkerProcess.

For the hosted SharePoint environment, this can be one of the best ways to deploy and maintain the solutions and sites.

There are many limitations in terms of accessing resources while you program against sandbox solutions in SharePoint. I am listing down some of them. You cannot:

    1)    System.IO, ADO.Net
    2)    Write to disk files
    3)    Oher site collection content
    4)    SPSecurity.RunWithElevatedPrivileges
    5)    Change threading model
    6)    Create visual web part
    7)    Access programmatic workflow
    8)    Timer Job
    9)    Web Application scoped features 

Here are certain items that you can do in sandbox solution environment

   1)    Web Parts (not visual)
   2)    Event receivers
   3)    Feature receivers
   4)    Custom SharePoint Designer Workflow Activity

     There are certain steps that farm administrator has to perform before setting up sandbox solution environment if entire set up is farm architecture. Farm administrator starts the sandbox service in each server in farm, configures the best load balancing service for sandbox solutions on each server and then set the resource quotas.

Read Part-2 for further reading.

Tuesday, December 20, 2011

The result is out

Hi


Finally the result is out. It seems people think that MS has to work harder when Google launches its OS.



Friday, December 16, 2011

Using the Dialog framework in SharePoint 2010 – Part 5


If you have not gone through Part-1 to Part-4 of this series, I would recommend you reading them first and then continue reading from here on.

In this post, we are going to see how we can pass parameters to model dialog window from a parent page.

We are going to take the same example which we had taken in previous posts.

Add a query string to a url parameter to the function.

    function OpenModalDialog() {        

        var sitename = 'SharePointkings';

        var options = {
            url: '/{site url}/_layouts/SharePointActivities/CustomDialog.aspx?sitename=' + sitename,
            tite: 'Open a custom dialog',
            allowMaximize: false,
            showClose: true,
            width: 750,
            height: 550,
             args:parameters,
            dialogReturnValueCallback: FunctionToCall
           
           
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }

If you would like to get an intellisence of Visual studio for SP.UI and SP.UI.Debug javascript file, then add a reference like this

<script type="text/javascript" language="javascript">

   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />
   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.js" />
</script>


And then when you type, you get the Intelisence.


And on the receiving model dialog page write down the following JavaScript functions in your model dialog page.

<script type="text/javascript" language="javascript">

   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />
   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.js" />

   if (typeof (_spBodyOnLoadCalled) == 'undefined' || _spBodyOnLoadCalled) {  
   
   window.setTimeout(AlertParametersPassedFromParent, 0);       
     }  
   else {

       _spBodyOnLoadFunctionNames.push("AlertParametersPassedFromParent");

       }

 function getQuerystring(key, default_) {
     if (default_ == null) default_ = "";
     key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
     var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
     var qs = regex.exec(window.location.href);
     if (qs == null)
         return default_;
     else
         return qs[1];
 }


 function AlertParametersPassedFromParent() {   

     var sitename = getQuerystring('sitename');

     alert(sitename);
   }
   

  </script>


When the page loads, we push the function AlertParametersPassedFromParent to execute and called a function called getQueryString which is actually logic to retrieve query string which is passed from the parent page. There is no ready method in JavaScript like server side methods. So we have to use our own method to get that.

And we have then just prompted the parameter value with alert to see if it has passed properly.

Here is what we get as an end result.




I hope you have enjoyed the entire series of model dialog framework. We will try and add some new content if we find anything interesting about model dialog more. Of course we encourage and appreciate our readers to share their experiences so that we all SharePoint working community get benefited from it.





Tuesday, December 13, 2011

Using the Dialog framework in SharePoint 2010 – Part 4


If you have not gone through Part-1 to Part-3 of this series, I would recommend you reading them first and then continue reading from here on.

In this post, we are going to see how we can pass parameters to a parent page from where we have opened up the model dialog.

We take the same example that we have in our previous post. I am not going to rewrite the entire code and pages. You can have a look at previous posts to gain the insight of it.

Open your aspx page and change the button click functionto following. As you can see we have declared an array of result and we have passed name and value pair of parameter values in it and then passed the entire results array as an object to our window.frameElement.commitpopup method.


  <script language="javascript" type="text/javascript">

      function OkButtonClicked() {

          var results = {

              message: "Custom message from a custom dialog page",
              passedvalue : "yes"
              
          };
        
          window.frameElement.commitPopup(results);

      } 


 </script>

So when the page is closed, we can now retrieve these parameters in the parent page. We can do so in following way.

Change the function on parent page code to this.

    function FunctionToCall(dialogResult, returnValue) {


        if (dialogResult == SP.UI.DialogResult.OK) {
            SP.UI.Notify.addNotification('OK was clicked' + ' \n Message : ' + returnValue.message.toString() + '\n passed value : ' + returnValue.passedvalue);
            

        }

        if (dialogResult == SP.UI.DialogResult.cancel)
            SP.UI.Notify.addNotification('Cancel was clicked!');
    }


What we are doing here is we get the returnvalue as results and can access the parameters as a property.

And when you build and deploy the solution and check, you get the result like this.


Rear Part-5 for further reading.

Thursday, December 8, 2011

Using the Dialog framework in SharePoint 2010 – Part 3


If you have not gone through Part-1 and Part-2 of this series, I would recommend you reading them first and then continue reading from here on.

In this post, we are going to create our own page and then open the page with dialog framework and then handle Ok and Cancel event to that just like how we saw in previous post with library upload page.

Open your own project in Visual studio 2010. If you have not created a project, create an empty SharePoint project and add new item an application page. Give it a name like CustomPage.aspx. Visual studio will automatically map the page to layout folder.

All the dialog pages that open in SharePoint 2010 inherits from dialog.master file which is in TEMPLATE/LAYOUTS folder. So we also need to modify our markup to inherit the page from dialog.master.

We can remove the dynamicmasterpage file attribute from the page tag. We will see about this attribute in some future post. As of now, we simply remove it.

Now because we have changed the master page URL and we are referring to a different master page, all the content place holders which are on the page may not be there in the master page that we are now referring.

So we need to make the change in the content place holders as well.

Remove all these content tags.


When you are not sure, you always should check the master page content place holder IDs and the content place holder IDs present in content page. If there is any misplaced place holder, then you will not be able to work on the page. The page will not be rendered.

Add following content place holders to the page.

<asp:Content ID="Content1"  ContentPlaceHolderID="PlaceHolderDialogHeaderPageTitle" runat="server">Custom Dialog Box</asp:Content>  
 <asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">  

 <SharePoint:CssRegistration ID="CR1" runat="server" Name="ows.css" />  

 <SharePoint:ScriptLink ID="SL1" Language="javascript" Name="core.js" runat="server" />  

  <SharePoint:FormDigest ID="FormDigest1" runat="server" />  

 </asp:Content>  

 <asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderDialogImage" runat="server">  

   <img src="/_layouts/images/allcontent32.png" alt="" />  

 </asp:Content>  

 <asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderDialogBodyHeaderSection" runat="server">  

 </asp:Content>  

 <asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderDialogDescription" runat="server">  

   <div id="selectTermDescription" class="none-wordbreak">  

     <table class="ms-dialogHeaderDescription">  

       <tbody>  

         <tr>  

           <td>Description Goes here</td>  

         </tr>  

      </tbody>  

     </table>  

   </div>  

 </asp:Content>  

 <asp:Content ID="Content8" ContentPlaceHolderID="PlaceHolderHelpLink" runat="server">  

   <!-- We can remove the default help link from here --> 

 </asp:Content>  

 <asp:Content ID="Content6" ContentPlaceHolderID="PlaceHolderDialogBodyMainSection" runat="server">  

   You can create the entire HTML here to work on a page. Table layouts, controls etc.

 </asp:Content> 


Use the same OpenModelDialog method in your web part or any button on your custom page to open this layouts page.

  function OpenModalBing()
    {
        var options = {
            url: '/sites/SP2010/_layouts/SharePointActivities/CustomDialog.aspx',
            tite: 'Open a custom dialog',
            allowMaximize: false,
            showClose: true,
            width: 750,
            height: 550,
            dialogReturnValueCallback: FunctionToCall 

                      
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }

function FunctionToCall(dialogResult, returnValue) {


        if (dialogResult == SP.UI.DialogResult.OK) {
            SP.UI.Notify.addNotification('OK was clicked');
            

        }

        if (dialogResult == SP.UI.DialogResult.cancel)
            SP.UI.Notify.addNotification('Cancel was clicked!');
    }


And this is what you get. You can see the default buttons OK and Cancel been added to the page. We thought have not hooked up any event to that.



Now because we have inherited our custom page from dialog master page hence we got the ok and cancel button on a page. We need to write a custom code to the ok and cancel button. So we need to hook up our code to ok button.

So let’s add some code to it. Open the cs file of our custom page and write down the following code to it.

public partial class CustomDialog : LayoutsPageBase
    {
        protected override void OnLoad(EventArgs e) {  

         if (Master is DialogMaster)  
         {  
             DialogMaster dialogMasterPage = Master as DialogMaster;

             dialogMasterPage.OkButton.Attributes.Add("onclick", "return OkButtonClicked();");  
         }  

         base.OnLoad(e);  

     }  

    }


Now we need to add this script to a aspx page. So open up aspx page and add the function script.

  <script language="javascript" type="text/javascript">

      function OkButtonClicked() {

          window.frameElement.commitPopup();     
      } 


 </script>


Now build and deploy the solution and see this in action.

Click cancel, the message appears and click ok, the message appears after clicking on cancel or ok. Window.frameelement.commitpopup closed the model dialog and passes the dialog option value based on ok or cancel button clicked.



Read further in Part-4 of this series.

Monday, December 5, 2011

Using the Dialog framework in SharePoint 2010 – Part 2



If you have not gone through Part-1 of this series, I would recommend you reading part-1 and then continue reading from here on.

In this post, we are going to see how to open built in SharePoint list page and react to Ok or Cancel button clicked on a page.

Let us enhance the example that we saw in previous post. We will add some more lines to code and this time we will bind a function that will execute after we close the window from clicking ok or cancel button.

So let’s modify the code.

<script  type="text/javascript">

    function OpenModalBing()
    {
        var options = {
            url: '/{your site}/_layouts/Upload.aspx?List={1F68B750-98D7-4523-8C3C-F662E1E2386F}&RootFolder=',
            tite: 'Add new document',
            allowMaximize: false,
            showClose: true,
            width: 750,
            height: 550,
            dialogReturnValueCallback: FunctionToCall 
           
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }  

    function FunctionToCall(dialogResult, returnValue) 
        

if(dialogResult == SP.UI.DialogResult.OK)
        SP.UI.Notify.addNotification('OK was clicked'); 
        
}

if(dialogResult == SP.UI.DialogResult.cancel) 
        SP.UI.Notify.addNotification('Cancel was clicked!'); 
}
   
</script>





What we are doing here is we are opening a upload window page in model dialog and then based on a user action we display a message. If user uploads a document and click on Save or ok then dialog returns the OK status and if user clicks on cancel then it returns the cancel status that we check in the JavaScript function.

dialogReturnValueCallback is the function that executes after the model dialog closes. When declaring that function we need to define two parameters. One is the result and the other is return value.

Here in this example, we are not using the return value but checking the default button clicks.

Here is what you can see in action. When cancel clicked and when OK clicked.









In next post, we will create our own page, will deploy the page and then opens that page via dialog framework and handle Ok and Cancel button click event on our own. 


Read Part-3 of this series to explore more.




Saturday, December 3, 2011

The result is out

The result is out

It seems that we have more XBox lovers than Play Station.





Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com