Friday, May 6, 2011

ECMAScript - How to get lists

OBJECTIVE
To show different ways to retrieve lists:
  1)       RETRIVING CURRENT LIST
   2)       RETRIVING LIST BY ‘TITLE’
  3)       RETRIVING LIST BY ‘ID’
  4)       RETRIVING ALL LISTS
   5)       RETRIVING A SINGLE PROPERTY FROM LISTS


1) RETRIVING CURRENT LIST
When you are working with lists (ex by Ribbon Button), sometime it is necessary to know the current list by client side.
If you are working with the standard view, you can use it:

  var list = SP.ListOperation.Selection.getSelectedList();

Instead if you are working in datasheet, you may have some problems. At the following link it is suggested a workaround http://msdn.microsoft.com/en-us/library/ff410971.aspx.
 


2) RETRIVING LIST BY ‘TITLE’
In this example it retrieves one list by Title from the Root Web.


<script type="text/ecmascript">

    SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');   

    function initialize() {
        var clientContext = new SP.ClientContext();
        var siteColl = clientContext.get_site();
        myweb = siteColl.get_rootWeb();
        this.list = myweb.get_lists().getByTitle('ListTitle'); //Edit the title
        clientContext.load(list);
        clientContext.executeQueryAsync(Function.createDelegate(this, GetList), Function.createDelegate(this, getFailed));
    }

    function GetList() {
            alert(list.get_title() + ': ' + list.get_id().toString());
    }
  
    function getFailed() {
        alert('Failed.');
    }
</script>



3) RETRIVING LIST BY ‘ID’
In this example it retrieves one list by Id from the Root Web.

<script type="text/ecmascript">

    SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');

    function initialize() {
        var value = SP.ListOperation.Selection.getSelectedList(); 
        var clientContext = new SP.ClientContext();
        var siteColl = clientContext.get_site();
        this.myweb = siteColl.get_rootWeb();
        this.list = myweb.get_lists().getById('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'); //Edit the ID
        clientContext.load(list);
        clientContext.executeQueryAsync(Function.createDelegate(this, GetList), Function.createDelegate(this, getFailed));
    } 

    function GetList() {
        alert(list.get_title() + ': ' + list.get_id().toString());
    } 

    function getFailed() {
        alert('Failed.');
    } 

</script>
 


4) RETRIVING ALL LISTS
In this example it retrieves all the lists in the Root Web

<script type="text/ecmascript">
   
    SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');   

    function initialize() {
        var clientContext = new SP.ClientContext();
        var siteColl = clientContext.get_site();
        myweb = siteColl.get_rootWeb();
        this.lists = myweb.get_lists();
        clientContext.load(lists);
        clientContext.executeQueryAsync(Function.createDelegate(this, GetLists), Function.createDelegate(this, getFailed));
    }

    function GetLists() {
        var listEnumerator = lists.getEnumerator();
        while (listEnumerator.moveNext()) {
            list = listEnumerator.get_current();
            alert(list.get_title());
        }
    }

    function getFailed() {
        alert('Failed.');
    }
</script>



5) RETRIVING A SINGLE PROPERTY FROM LISTS
When you retrieve a list or a collection of list, you can choice what properties you want.  In this example it retrieves the ID for all the lists in the root web.
<script type="text/ecmascript">
   
    SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');   

    function initialize() {
        var clientContext = new SP.ClientContext();
        var siteColl = clientContext.get_site();
        myweb = siteColl.get_rootWeb();
        this.lists = myweb.get_lists();
        clientContext.load(lists, 'Include(Id)');
        clientContext.executeQueryAsync(Function.createDelegate(this, GetLists), Function.createDelegate(this, getFailed));
    }

    function GetLists() {
        var listEnumerator = lists.getEnumerator();
        while (listEnumerator.moveNext()) {
            list = listEnumerator.get_current();
            alert(list.get_id());
        }
    }
 
    function getFailed() {
        alert('Failed.');
    }  
</script>


Monday, May 2, 2011

Sandbox Solution Limitations: Workarounds.

1) Cannot use Visual Web Part    
      You can!
      Install the Visual Studio 2010 SharePoint Power Tools:         
     http://visualstudiogallery.msdn.microsoft.com/8e602a8c-6714-4549-9e95-f3700344b0d9/
     ant you will find a new "Visual Web Part (Sandboxed)" template.


 
2) Cannot deploy files on layouts folder
     You can use a Module to deploy files on your application, without using Document Library.


     You can read the files appending the Module URL to your site collection URL:
     http://[MyHostName]/[MySiteCollection]/MyFiles/js/jquery-1.4.4.min.js

3) Cannot use Application Pages with code behind
      You can deploy an application page using a Module and add a Sandboxed Web Part to use code behind.
       SharePoint offers a specific class name <WebPartPages:SPUserCodeWebPart > to archives this goal.
       At the following link you can find a good example:
       http://www.wictorwilen.se/Post/Custom-application-pages-in-the-SharePoint-2010-Sandbox.aspx