Friday, 25 December 2015

Dynamically Adding Dropdown Using REST API and jQuery

Introduction
SharePoint 2013 has greatly expanded the REST services available to developers. With this, we have much more SharePoint functionality exposed via CSOM and Web Services. Also, all of the new REST Services are in SharePoint 2013. SharePoint 2013 was able to provide me with a REST API, I could call with jQuery ajax requests and this was exactly what I wanted.

REST Services-High Level Overview
Let's start out with our basic get commands in REST. The following is a list of the basic commands used to get List Items from a SharePoint List using the SharePoint 2013 REST Services.



Imagine
In my example, I'm accessing a Custom list (of countries) and output the result binding it to a dynamic dropdown. I have sorted by a column in ascending only. Using SharePoint's REST API lets us add these filters to our request. The results are given to us as a JSON object that we can then loop through and insert into a dropdown runtime. I also used a modular pattern to structure my code. We can generate our REST request. _spPageContextInfo is a SharePoint object that gives us useful information about the page and site we're on, including the base URL of our site.

After successfully getting our list information, we just need to loop through our data, put it in a dropdown and then inserted into our predefined container element. jQuery helps make this an easy process.



Let's proceed
Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:
  1. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>  
  2. <script>  
  3.   
  4.     $(document).ready(function () {  
  5.         countriesDrpDownBind();  
  6.       });  
  7.     function countriesDrpDownBind() {  
  8.         var listName = "countries";  
  9.         var url = _spPageContextInfo.webAbsoluteUrl;  
  10.   
  11.         getListItems(listName, url, function (data) {  
  12.             var items = data.d.results;  
  13.              
  14.             var inputElement = '<select id="drpcountries"> <option  value="">Select</option>';  
  15.                // Add all the new items  
  16.             for (var i = 0; i < items.length; i++) {  
  17.                  var itemId = items[i].Title,  
  18.                    itemVal = items[i].Title;  
  19.                  inputElement += '<option value="' + itemId + '"selected>' + itemId + '</option>';  
  20.                 
  21.                }  
  22.                 inputElement += '</select>';  
  23.                 $('#divisiondrp').append(inputElement);  
  24.   
  25.               $("#drpcountries").each(function () {  
  26.                 $('option', this).each(function () {  
  27.   
  28.                     if ($(this).text() == 'Select') {  
  29.                         $(this).attr('selected', 'selected')  
  30.                     };  
  31.                 });  
  32.             });  
  33.                // assign the change event to provide an alert of the selected option value  
  34.               $('#drpcountries').on('change', function () {  
  35.               alert($(this).val());  
  36.                   });  
  37.              
  38.           }, function (data) {  
  39.             alert("Ooops, an error occured. Please try again");  
  40.         });  
  41.     }  
  42.     // READ operation  
  43.     // listName: The name of the list you want to get items from  
  44.     // siteurl: The url of the site that the list is in.  
  45.     // success: The function to execute if the call is sucesfull  
  46.     // failure: The function to execute if the call fails  
  47.     function getListItems(listName, siteurl, success, failure) {  
  48.         $.ajax({  
  49.             url: siteurl + "/_api/web/lists/getbytitle('" + listName + "')/items?$orderby=Title asc",  
  50.             method: "GET",  
  51.             headers: { "Accept": "application/json; odata=verbose" },  
  52.             success: function (data) {  
  53.                 success(data);  
  54.             },  
  55.             error: function (data) {  
  56.                 failure(data);  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61. </script>  
  62. Division  
  63. <div id="divisiondrp"></div>  
Finally the result looks as in the following:

Friday, 18 September 2015

Adding Custom Ribbon Tab To the SharePoint 2013 Server Ribbon

Introduction 
In this article we will explore to how to create a custom tab in SharePoint 2013 Custom Action Ribbon.

I wanted to create a Custom Action Tab in my SharePoint Hosted app. Consider that SharePoint 2013 already provides a home that may be suitable for your customization, for list pages at least; the Custom Commands tab. It only appears on list pages and contains just one lonely button under normal circumstances. This tab is actually the post-upgrade home for any SharePoint 2007 CustomActions you had, assuming you don't introduce changes during your upgrade process, any CustomActions that didn't target the ECB will end up here. In any case, it could help avoid tab proliferation so don't forget it when building your customizations.

Solutions
  1. Add a ribbon custom action for the App project that you have created. Right click on the project [Add] [New Item] [Ribbon Custom Action] and enter the Name and click on the Add button. It will start the wizard for creating a custom action for the Ribbon control.

    custom action for Ribbon
  2. In this wizard select the host web where you want to expose the custom action. In other words your custom action will be available in that host web.

    The custom action scoped can be a List template or a List instance or None. In this example I am selecting List template and the item is the custom action scoped to be "Custom list".

    custom list
  3. Selected "Ribbon.ListItem.Actions". What this does is, it will show the custom action under the Items section of the List Ribbon control.

    ribbon list item
  4. The following is the Custom Ribbon XML that I created for the Custom Tab. Refer to the Ribbon XML Schema for more options: Server Ribbon schema.
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
    3.   
    4.   <CustomAction Id="Team Collaborate"  
    5.                 RegistrationType="List"  
    6.                 RegistrationId="100"  
    7.                 Location="CommandUI.Ribbon"  
    8.                 Sequence="10001"  
    9.                 Title="Team Collaborate"  
    10.               >  
    11.     <CommandUIExtension>  
    12.       <CommandUIDefinitions>  
    13.         <CommandUIDefinition Location="Ribbon.Tabs._children">  
    14.           <Tab Id="Team Collaborate.Tab"  
    15.                 Title="Team Collaborate"  
    16.                 Description="Special Risk Tracking Functionality"  
    17.                 Sequence="550">  
    18.   
    19.             <Scaling Id="Team Collaborate.Tab.Scaling">  
    20.               <MaxSize Id="Team Collaborate.Tab.EnterpriseRiskGroup.MaxSize"  
    21.                         GroupId="Team Collaborate.Tab.EnterpriseRiskGroup"  
    22.                         Size="OneLarge"/>  
    23.               <Scale Id="Team Collaborate.Tab.EnterpriseRiskGroup.Scaling.CustomTabScaling"  
    24.                       GroupId="Team Collaborate.Tab.EnterpriseRiskGroup"  
    25.                       Size="OneLarge" />  
    26.             </Scaling>  
    27.             <Groups Id="Team Collaborate.Tab.Groups">  
    28.               <Group Id="Team Collaborate.Tab.EnterpriseRiskGroup"  
    29.                       Description="Functions that can be performed on Enterprise Risks"  
    30.                       Title="Sharepoint Consulting"  
    31.                       Sequence="10"  
    32.                       Template="Ribbon.Templates.OneLargeExample">  
    33.                 <Controls Id="Team Collaborate.Tab.EnterpriseRiskGroup.Controls">  
    34.                   <Button Id="Team Collaborate.Tab.EnterpriseRiskGroup.Notify"  
    35.                           Command="Team Collaborate.EnterpriseRisk"  
    36.                           Sequence="10"  
    37.                           Image16by16="_layouts/15/TeamCollaborate.png"  
    38.                           Image32by32="_layouts/15/TeamCollaborate.png"  
    39.                           Description="Selected Largest Team of Certified SharePoint developers."  
    40.                           LabelText="Expert Sharepoint"  
    41.                           TemplateAlias="cust1"/>  
    42.                 </Controls>  
    43.               </Group>  
    44.   
    45.             </Groups>  
    46.           </Tab>  
    47.         </CommandUIDefinition>  
    48.         <CommandUIDefinition Location="Ribbon.Templates._children">  
    49.           <GroupTemplate Id="Ribbon.Templates.OneLargeExample">  
    50.             <Layout Title="OneLarge" LayoutTitle="OneLarge">  
    51.               <Section Alignment="Top" Type="OneRow">  
    52.                 <Row>  
    53.                   <ControlRef DisplayMode="Large" TemplateAlias="cust1" />  
    54.   
    55.                 </Row>  
    56.               </Section>  
    57.             </Layout>  
    58.           </GroupTemplate>  
    59.         </CommandUIDefinition>  
    60.       </CommandUIDefinitions>  
    61.   
    62.       <CommandUIHandlers>  
    63.         <CommandUIHandler Command="Team Collaborate.EnterpriseRisk"  
    64.                         CommandAction="~remoteAppUrl/Pages/TeamCollaborate.aspx?{StandardTokens}&Source={Source}&ListURLDir={ListUrlDir}&list={ListId}&SelectedItems={SelectedItemId}" />  
    65.   
    66.       </CommandUIHandlers>  
    67.   
    68.     </CommandUIExtension>  
    69.   
    70.   </CustomAction>  
    71. </Elements>  
  5. Build and publish App and add into the apps in the Testing Library and deploy; Trust it.

    Testing Library

    app in testing

    Testing Library and deploy

    The following shows the final output Custom Ribbon Tab:

    Custom Ribbon
SummaryThe ribbon is a key building block for SharePoint 2013 solutions and if you see a ribbon tab for a Custom List on the Ribbon Control Bar then it can be customized.

Custom Ribbon Action and Set the Customize Icon to Ribbon Button in a SharePoint Hosted App

Introduction 
In this article you will learn how to set the Customize Icon to Ribbon Button in a SharePoint Hosted App.

This article explains the Custom Ribbon Action to set the Custom Image to a Ribbon button and that is absolutely awesome! I was creating a Custom Action in my SharePoint Hosted app. I wanted to add a Button into the Ribbon of any Custom List to my App Web and do some work based on List Information. I started by adding a Custom Ribbon Action.

Custom Ribbon button

create Custom action for Ribbon

ribbon listItem

You can see the following code snippet of elements.xml of the Custom Ribbon Action that will be generated for you based on your settings selected in the wizard.

selected in the wizard

Have a look at the highlighted code in the previous block. You will notice that the icon of the button is a placeholder you have the ability to change. The next screenshot shows the button with the placeholder icon:

placeholder icon

Solution:
A better workaround is to use Data URIs that is a way of embedding the actual image data in the src attribute of the img tag. The only limitation is browser support for example.

The code after applying this workaround will be like this:

applying this workaround

The following is the final output of the Customize Icon to Ribbon button.

Ribbon button

Note: There are many image data URI generators online like this one.

SharePoint: Customization Evolution

Introduction

In this article we explore SharePoint’s customization evolution from SharePoint 2007 popularly known as MOSS to SharePoint 2010 and then towards the current version in use, SharePoint 2013.
In all the versions there are various architecture-level upgrades highlighted in this article.

customization evolution from SharePoint

sharepoint code

Full trust solutionsYou're missing control, since we basically grant full access rights for the code across our application and in servers. In previous versions you could have limited provide access, but that was difficult to do and was not used that often. In general deployment always causes a service break on the servers, since there's really no way to update the server without at least recycling the application pool. 

SandboxOur customization story basically grew up a little bit or from an administrative perspective when we gained some control. This was introduced in 2010 but was too strict from the server-side API level that developers have been used to use and client-side APIs were also so limited that even though you could have implemented some.

App modelThe Model is growing up with key objectives being on control, trust and management.

SharePoint Application Evolution:
  • 2007: Everything is an application

    • Push to embrace SharePoint for LOB applications
    • Experience was to write custom server-side code
  • 2010: Choose the right application

    • SharePoint and Developer Tools come together
    • Silverlight, CSOM and other new capabilities
    • Partially trusted code reduces impact and risk to the Farm
  • 2013: Redefine application as app

    • Client-side experience via HTML or iFrame
    • Leverage CSOM and REST APIs from Azure and other clients
    • Investments in app discovery and management via SharePoint store

SharePoint 2013 - Uploading Multiple Attachments To The New Item On List Using JSOM And REST API

  Introduction In this article, we will explore how we can attach multiple attachments to the new item on list using JSOM and REST API. Ther...