Friday, 15 November 2013

Add Custom Web Parts to a page and Connect them using PowerShell


These days I am working heavily on SharePoint CSOM and BCS (Business Connectivity Services). I have created a sandbox package with multiple custom business data and html form data web parts.

The reason why I am writing this blog is to share with you some of the cool stuffs I did with PowerShell in the project.

I have written many automation scripts to create web part page, add web parts to this page and then connect to them on a single click using PowerShell.

First thing first…

1.       Create a web part page using PowerShell:

Function CreateWebPartPage
{
param($title,$name,$pageLayout)
Write-Host "***************Creating Web part page: "$title -ForegroundColor Yellow
if([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)){
$pubWeb =[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$name+=".aspx";
$layout = $pubWeb.GetAvailablePageLayouts() | Where { $_.Name -eq $pageLayout}
$newPage = $pubWeb.AddPublishingPage($name, $layout);
$newPage.Title=$title;
$newPage.Update()
Write-Host "***************Page is created successfully" -ForegroundColor Yellow
}
Else{
Write-Host "***************Not a publishing web" -ForegroundColor Yellow
}}
$Url = $(Read-Host -Prompt "Enter site url:")
$web = Get-SPWeb $Url
CreateWebPartPage "Test Page" "Test" "BlankWebPartPage.aspx"
$web.Dispose();


2.       Add web part to this page:

$Url = $(Read-Host -Prompt "Enter site url:")
$web = Get-SPWeb $Url
$web.AllowUnsafeUpdates=$true
function Add-WebPartToPage($pageUrl, $webpartzone,$index,$fileName)
{
$webPartGallery = $web.Lists["Web Part Gallery"]
Write-Host "Searching webpart $fileName in web part gallery" -ForegroundColor Yellow
if($webPartGallery -eq $null)
{
Write-Host("Unable to retrieve Webpartgallery");
}
$webpart = $null;
$webpart=$webPartGallery.Items | ? { $_.Title -eq $fileName}
if($webpart -eq $null) {
Write-Host("Unable to retrieve webpart: $fileName") -ForegroundColor Red
}
else {
Write-Host("----------Adding Webpart--------")-ForegroundColor Yellow
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$errorMsg = "";
$xmlReader = New-Object System.Xml.XmlTextReader($webpart.File.OpenBinaryStream());
$webpart = $webpartmanager.ImportWebPart($xmlReader,[ref]"Error")
$webpartmanager.AddWebPart($webpart, $webpartzone, $index);
Write-Host("Webpart is added successfully") -ForegroundColor Green ;
}
}
$SiteURL =$Url
#---------------Test Page----------------------------
$PageName="Test.aspx"
$page=$web.lists["Pages"].Items | ? {$_.Name -eq $PageName}
$page.File.CheckOut();
Add-WebPartToPage "$SiteURL/Pages/$PageName" "Header" 0 "My Custom WebPart"
$page.File.CheckIn("Added Web part to a page");
$page.File.Approve("Added Web part to a page");


3.       Connect web parts in a page: In this script I have shown how we can connect

a.       Query String filter Web Part with Business Data List Web Part

b.      Query String filter Web Part with Business Data Item Web Part

c.       Business data List Web Part with Business Data Item Builder Web Part

 

function Connect-QueryStringFilterWP-To-BCSListWP($pageUrl,$fileName,$providerWP,$consumerArrayValue,$providerArrayValue,$consumerConnectionId)
{
$webPartGallery = $web.Lists["Web Part Gallery"]
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
Write-Host "Searching Consumer webpart $fileName in web part gallery" -ForegroundColor Yellow
if($webPartGallery -eq $null)
{
Write-Host("Unable to retrieve Webpartgallery");
}
$webPartConsumer = $webpartmanager.WebParts | Where {$_.Title -eq $fileName}
Write-Host "Searching Consumer webpart $providerWP in web part gallery" -ForegroundColor Yellow
$webPartProvider = $webpartmanager.WebParts | Where {$_.Title -eq $providerWP}
$conWP = $webpartmanager.GetConsumerConnectionPoints($webPartConsumer)
$provWP = $webpartmanager.GetProviderConnectionPoints($webPartProvider)
$conWP=$conWP[$consumerConnectionId]
$provWP=$provWP["ITransformableFilterValues"]
$trans = New-Object Microsoft.SharePoint.WebPartPages.TransformableFilterValuesToParametersTransformer
$consumerArray=@($consumerArrayValue)
$providerArray=@($providerArrayValue)
$trans.ProviderFieldNames=$providerArray
$trans.ConsumerFieldNames=$consumerArray
$newCon = $webpartmanager.SPConnectWebParts($webPartProvider,$provWP,$webPartConsumer,$conWP,$trans)
$webpartmanager.SPWebPartConnections.Add($newCon);
$webpartmanager.Dispose()
Write-Host "Webparts are connected successfully" -ForegroundColor Green
}
function Connect-QueryStringFilterWP-To-BCSListItemWP($pageUrl,$fileName,$providerWP,$consumerConnectionId)
{
$webPartGallery = $web.Lists["Web Part Gallery"]
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
Write-Host "Searching Consumer webpart $fileName in web part gallery" -ForegroundColor Yellow
if($webPartGallery -eq $null)
{
Write-Host("Unable to retrieve Webpartgallery");
}
$webPartConsumer = $webpartmanager.WebParts | Where {$_.Title -eq $fileName}
Write-Host "Searching Consumer webpart $providerWP in web part gallery" -ForegroundColor Yellow
$webPartProvider = $webpartmanager.WebParts | Where {$_.Title -eq $providerWP}
$conWP = $webpartmanager.GetConsumerConnectionPoints($webPartConsumer)
$provWP = $webpartmanager.GetProviderConnectionPoints($webPartProvider)
$conWP=$conWP[$consumerConnectionId]
$provWP=$provWP["ITransformableFilterValues"]
$trans = New-Object Microsoft.SharePoint.Portal.WebControls.TransformableFilterValuesToEntityInstanceTransformer
$newCon = $webpartmanager.SPConnectWebParts($webPartProvider,$provWP,$webPartConsumer,$conWP,$trans)
$webpartmanager.SPWebPartConnections.Add($newCon);
$webpartmanager.Dispose()
Write-Host "Webparts are connected successfully" -ForegroundColor Green
}
function Connect-BCSWebParts($pageUrl,$fileName,$providerWP){
$webPartGallery = $web.Lists["Web Part Gallery"]
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
Write-Host "Searching Consumer webpart $fileName in web part gallery" -ForegroundColor Yellow
if($webPartGallery -eq $null)
{
Write-Host("Unable to retrieve Webpartgallery");
}
$webPartConsumer = $webpartmanager.WebParts | Where {$_.Title -eq $fileName}
Write-Host "Searching Consumer webpart $providerWP in web part gallery" -ForegroundColor Yellow
$webPartProvider = $webpartmanager.WebParts | Where {$_.Title -eq $providerWP}
$conWP = $webpartmanager.GetConsumerConnectionPoints($webPartConsumer)[0]
$provWP = $webpartmanager.GetProviderConnectionPoints($webPartProvider)[0]
$newCon = $webpartmanager.SPConnectWebParts($webPartProvider,$provWP,$webPartConsumer,$conWP)
$webpartmanager.SPWebPartConnections.Add($newCon);
$webpartmanager.Dispose()
Write-Host "Webparts are connected successfully" -ForegroundColor Green
}
function setupPageConnections ($pageName) {
Write-Host "Connecting webparts for page: $pageName" -ForegroundColor Yellow
$page=$web.lists[$LibName].Items | ? {$_.Name -eq $PageName}
$page.File.CheckOut();
setupWebPartsConnections $pageName
$page.File.CheckIn("Webparts are connected");
$page.File.Approve("Webparts are connected");
}
function setupWebPartsConnections ($pageName) {
Connect-BCSWebParts "$SiteURL/$LibName/$PageName" "{Custom Webpart Title}" "Business Data Item Builder" #{Custom Webpart Title} with title of custom web part
Connect-QueryStringFilterWP-To-BCSListWP "$SiteURL/$LibName/$PageName" "My Custom WebPart" "{Query String Filter WebPart tilte}" "{Custom Webpart filter Name}" "QueryString" "BDWP Query Values"
Connect-QueryStringFilterWP-To-BCSListItemWP "$SiteURL/$LibName/$PageName" "{Custom Webpart Title}" "{Query String Filter WebPart tilte}" "BDWP Item"
}
$Url = $(Read-Host -Prompt "Enter site url:")
$web = Get-SPWeb $Url
$web.AllowUnsafeUpdates=$true
$LibName = "Pages";
$SiteURL =$Url
setupPageConnections "Test.aspx"
$web.Update();
$web.AllowUnsafeUpdates=$false
$web.Dispose()


Have Fun!

Isha Jain

Thursday, 25 April 2013

Paste content into SharePoint with no formatting

Hello Again,

These days I am working heavily into SharePoint Ribbon customization. If you have not read my previous blog on this, I would recommend you to read Previous Ribbon Blog before reading this one.

SharePoint 2010 brings the new evolution in user interface and have brought common functionalities into same page by introducing the Ribbon concept.

In SharePoint user is allowed to copy content from various sources like email, office applications like word, excel  and paste into SharePoint.

By default there are two ways to paste content into SharePoint from ribbon.


1. Paste (paste content with styles and formatting)
2. Paste palintext (paste content without styles but keeps the formatting)


However; pasting the content even as Plain text from word and other sources into SharePoint  introduces breaks and other markup issues in Html.

Paste  (format:break line and color:Blue)
Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | out-null
Write-Host "Hello All"

Paste Plaintext (only format:break line)
Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | out-null
Write-Host "Hello All"

I wanted to achive something shown below.

Paste with no formatting (no format no color)
Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | out-null Write-Host "Hello All"



I have created a sandbox solution and added element.xml as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Location="CommandUI.Ribbon" Id="AddNewPaste">
-<CommandUIExtension><CommandUIDefinitions>
<!--Add Paste with no formatting to the Paste Group -->
<CommandUIDefinition Location="Ribbon.EditingTools.CPEditTab.Clipboard.Paste.Menu.Paste.Controls._children">
<Button Id="Ribbon.Inset.CustomGroup.Paste"
Alt="Paste as plain text with no formatting"
ToolTipTitle="Paste as plain text with no formatting"
ToolTipDescription="Paste the contents of clipboard as plain text with no formatting."
Sequence="30" LabelText="Paste with no formatting"
Image16by16="_layouts/images/Paste.gif"
Image32by32="_layouts/images/PasteHH.png"
TemplateAlias="o1" Command="Ribbon.Paste"/>
</CommandUIDefinition></CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="Ribbon.Paste" CommandAction="javascript:pasteAsPlainTextWithNoFormatting();"/>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>
view raw gistfile1.xml hosted with ❤ by GitHub
On click of this flyout anchor menu I have called a javascript function as defined in commandaction attribute in the command handler.
function pasteAsPlainTextWithNoFormatting() {
var cursorLocation = RTE.Cursor.get_range();
var copiedData = window.self.clipboardData.getData('Text'); //Copy content from clipboard
var copiedDataSpan = document.createElement('span');
copiedDataSpan.setAttribute('id', 'copiedData');
copiedDataSpan.innerText = copiedData; //add clipboard content to new span element
cursorLocation.deleteContent();
cursorLocation.insertBefore(copiedDataSpan); //add the content with the span element at the cursor location
var rawContent = $ribbon('#copiedData').html(); //find that span
var cleanContent = rawContent.replace(/<[^>]*>/gi, ''); //remove all the formatting
$ribbon('#copiedData').html(cleanContent);//add the content back
RTE.RteUtility.removeNodeAndKeepChildNodes(copiedDataSpan);//remove the span by keeping the data
RTE.Cursor.update(); //update the cursor position
}
view raw gistfile1.js hosted with ❤ by GitHub


Hope you find this useful.
Cheers,
Isha Jain

Friday, 5 April 2013

How to find a SharePoint field from GUID

Hello,

I have been in a scenario where I need to find one of the SPFields from its GUID. Its again PowerShell who saves my time.


$site = Get-SPSite -Identity "http://Site"
$web=$site.RootWeb
$field=$web.Fields[[Guid]"E9359D15-261B-48f6-A302-01419A68D4DE"]

That's it, I am done!!!

Cheers,
Isha Jain

Monday, 25 March 2013

Find all the sites where the feature is activated

Hi,

I have been struggling how to find out the lists of sites where my feature is deployed.

Below PowerShell command which helped me.

Get-SPSite -limit ALL -WebApplication "http://demo.com.au" | Get-SPWeb -limit ALL | foreach{ Write-Output $_.Url; $_.features["a372b122-7c5b-4141-92a1-61979629a106"] }> c:\output.txt
view raw gistfile1.ps1 hosted with ❤ by GitHub


Instead of  using "Write-Host", I have used "Write-Output" which will give output in my output.txt file.

Note: Write-Host will write the output in the console while Write-Output helps to write the output in some file.

Hope this is useful.
Cheers,
Isha

Monday, 11 March 2013

Fetch Rss feeds into SharePoint List

I  had been into a scenario where I need to search the results from one of the Rss feeds into SharePoint search results page.

This means the data from this Rss feeds had to be into SharePoint content database and then SharePoint search crawler will crawl the content and made the content search able into the results page.

So I came up with a design of fetching the data from the Rss feeds into the SharePoint list in an hourly basis and will display the top 10 results into the home page and user can search on this Rss feeds.

In this design I need
1. Rss Feed link (http://feeds.bbci.co.uk/news/rss.xml)
2. SharePoint Custom list which will store the content from Rss feed.
3.Content Editor web part (CEWP) to display the top 10 results in to the home page.
4.Fetch the latest results into the SharePoint list in every one hour.

I am using SPServices to add/update the Rss Feeds into SharePoint list. In order to know more on SPServices and how to perform CRUD operations, Please visit my blog on CRUD operations using SPServices.

Step1: Fetch the Rss Feeds

At First, Created  a Custom List called RSS Feed with the custom columns Content, PublishedDate, FeedLink and Title.

function fetchRSSFeed()
{
$.ajax({
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=-1&callback=?&q=' + encodeURIComponent('http://feeds.bbci.co.uk/news/rss.xml'),
dataType: 'json',
success: function(data){
for (var i=0; i <data.responseData.feed.entries.length; i++)
{
var entry = data.responseData.feed.entries[i];
var entryDate = new Date(entry.publishedDate);
var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
var feedLink = encodeURIComponent(entry.link);
var content = $('<div/>').text(entry.content).html(); //Encode the content
// Add feeds to SharePoint List if not exists
AddItemToList(entry.title,feedLink,content,pubDate);
}}});
}
view raw gistfile1.js hosted with ❤ by GitHub
So what I am doing here is, Calling Google feed api using  jQuery ajax to fetch the Rss feed by passing query parameters, q as Rss Feed url link and num to -1 to fetch all the results back into json object and then iterating each object and storing into SharePoint list.

Step2:Store the feeds into SharePoint List

function AddItemsToList(title,feedLink,content,pubDate)
{
$().SPServices({
operation: 'UpdateListItems',
webURL: 'http://demo',
listName: 'RSS Feed',
updates: '<Batch OnError="Continue">' +
'<Method ID="1" Cmd="New">' +
'<Field Name="Title">'+ title +'</Field>' +
'<Field Name="FeedLink">'+ feedLink +'</Field>' +
'<Field Name="Content">'+ content +'</Field>' +
'<Field Name="PublishedDate">'+ pubDate +'</Field>' +
'</Method>' +
'</Batch>',
completefunc: function(xData, Status)
{}
});
}
view raw gistfile1.js hosted with ❤ by GitHub
Step3: Display the content from SharePoint List in the home page using CEWP.

$().SPServices({
operation: "GetListItems",
async: false,
listName: "RSS Feed",
CAMLQuery: "<Query><OrderBy><FieldRef Name='PublishedDate' Ascending='False' /></OrderBy></Query>",
CAMLRowLimit: 10,
completefunc: ProcessData
});
view raw gistfile1.js hosted with ❤ by GitHub
Step4: Fetch the latest feed in every hour.
function Rotation()
{
rotateInterval= setInterval(function() { fetchRSSFeed()}, 3600000 ); //Execute every one hour
}
view raw gistfile1.js hosted with ❤ by GitHub

Hope you find this blog useful.

Cheers,
Isha Jain

Sunday, 10 March 2013

SharePoint Ribbon Customization

Ribbon is one of the new features introduced in SharePoint 2010. After office client, Microsoft introduced ribbon in the similar fashion in SharePoint.

Ribbon in SharePoint 2010 provides the common place for most of the controls that you will need, Ribbon  is divided into basic four sections as shown in Fig 1 below.






1. Tab
2. Group
3. Controls
4. Contexual Tab









The architecture of Ribbon allows you to customize by creating xml files.

Inorder to understand how SharePoint implements its Ribbon, there is a XML element file at
%Program Files %\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\GLOBAL\XML\CMDUI.XML

If you open this xml file, you will see that its a huge file. Don't get afraid with this huge file.
Lets understand how SharePoint implements the Ribbon functionality.

The main element in the "CMDUI.xml" file is the "CommandUI" element, this element holds all the controls and the templates for the Ribbon.

<?xml version="1.0" encoding="utf-8"?>
<CommandUI xmlns="http://schemas.microsoft.com/sharepoint/">
<Ribbon ...
</Ribbon>
<Templates>
<RibbonTemplates Id="Ribbon.Templates">
<GroupTemplate Id="Ribbon.Templates.ClipboardGroup">
...
</RibbonTemplates>
<Templates>
</CommandUI>
view raw gistfile1.xml hosted with ❤ by GitHub
Ribbon element holds all the controls like Tabs, Group, Controls, Contextual Tab.

Templates element has GroupTemplate element. Each GroupTemplate element specifies the mode in which the controls within that group will be rendered.

<GroupTemplate Id="Ribbon.Templates.Flexible2">
<Layout Title="LargeLarge">
<OverflowSection Type="OneRow" TemplateAlias="o1" DisplayMode="Large"/>
<OverflowSection Type="OneRow" TemplateAlias="o2" DisplayMode="Large"/>
</Layout>
<Layout Title="LargeMedium">
<OverflowSection Type="OneRow" TemplateAlias="o1" DisplayMode="Large"/>
<OverflowSection Type="ThreeRow" TemplateAlias="o2" DisplayMode="Medium"/>
</Layout>
.....
view raw gistfile1.xml hosted with ❤ by GitHub
As you can see in the ribbon image Fig 1, some of the controls are rendered as large icons whereas  others like "Page History", "Delete Page" etc. (under Manage group) are rendered in medium size icons. All of these are controlled by GroupTemplate element.


Now, Lets have a close look at Ribbon element. Ribbon element has two child elements "Tabs" and "ContexualTabs". Each of these element further has Groups and Controls element which hold the html contols like label, textbox , combobox, dropdown. etc.

<Tabs Id="Ribbon.Tabs">
<Tab Id="Ribbon.ListForm.Display" Sequence="300" Command="Ribbon.ListForm.Display" Description="" Title="$Resources:core,cui_TabListFormDisplayTitle;">
...
<Groups Id="Ribbon.ListForm.Display.Groups">
...
</Groups>
</Tab>
</Tabs>
<ContextualTabs Id="Ribbon.ContextualTabs">
<ContextualGroup Id="Ribbon.EditingTools" Sequence="100" Color="Orange" Command="EditingToolsContextualGroup" ContextualGroupId="EditingTools" Title="$Resources:core,cui_TabGroupEditingTools;">
<Tab Id="Ribbon.EditingTools.CPEditTab" Sequence="100" Command="CPEditTab" Description="" Title="$Resources:core,cui_TabWikiEditTitle;">
...
<Groups Id="Ribbon.EditingTools.CPEditTab.Groups">
...
</Groups>
</Tab>
<Tab Id="Ribbon.EditingTools.CPInsert" Sequence="200" Command="InsertTab" Description="" Title="$Resources:core,TabPageInsertTitle;">
...
<Groups Id="Ribbon.EditingTools.CPInsert.Groups">
...
</Groups>
</Tab>
</ContexualGroups>
</ContextualTabs>
view raw gistfile1.xml hosted with ❤ by GitHub
I hope you have got the clear understanding of the CMDUI.xml file.

Now lets try to customize the ribbon. In this example I am going to add the caption to the table when a user inserts the table into the page. 

When a user edits the page and inserts the table into the page, "Table Tools" contextual tab shown below is displayed. What I am trying to achieve using this custom control is, when user enters some text into this highlighted textbox and hits save,  I am adding the Caption child element to the table. This is achieved by specifying the custom .js code into the command handler element.


Adding any custom controls into the ribbon can be achieved by creating custom action element.xml file in SharePoint 2010.

The CustomAction element has two main elements: CommandUIDefinitions and CommandUIHandlers.

CommandUIDefinitions hold all the custom controls definitions and the CommansUIHandlers hold the actions against each controls.

<CustomAction Location="CommandUI.Ribbon" Id="AddCaptionButton">
-<CommandUIExtension>
<!--Add Caption for Table -->
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Table.Layout.Summary.Controls._children">
<Label Id="Ribbon.Table.CaptionLabel" TemplateAlias="o1" LabelText="Caption:" Sequence="30" />
</CommandUIDefinition>
<CommandUIDefinition Location="Ribbon.Table.Layout.Summary.Controls._children">
<TextBox Id="Ribbon.Table.Caption" TemplateAlias="o2" Sequence="40" Command="Ribbon.Table.SetTableCaption" QueryCommand="Ribbon.Table.QueryTableCaption"/>
</CommandUIDefinition>
<!--Add Caption for Table End-->
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="Ribbon.Table.SetTableCaption" CommandAction="javascript:
if(RTE.CanvasEvents.getNodeFromEvent('Table').firstChild.nodeName.toLowerCase() !='caption')
{
var elementCaption=document.createElement('caption');
var elementCaptionText=document.createTextNode(document.getElementById('Ribbon.Table.Caption').value);
elementCaption.appendChild(elementCaptionText);
RTE.CanvasEvents.getNodeFromEvent('Table').insertBefore( elementCaption , RTE.CanvasEvents.getNodeFromEvent('Table').firstChild);
}
else
{
RTE.CanvasEvents.getNodeFromEvent('Table').firstChild.innerHTML=document.getElementById('Ribbon.Table.Caption').value;
};"></CommandUIHandler>
<CommandUIHandler Command="Ribbon.Table.QueryTableCaption" CommandAction="javascript:
if(RTE.CanvasEvents.getNodeFromEvent('Table').firstChild.nodeName.toLowerCase() =='caption')
{
document.getElementById('Ribbon.Table.Caption').value=RTE.CanvasEvents.getNodeFromEvent('Table').firstChild.innerHTML;
}
else
{
document.getElementById('Ribbon.Table.Caption').value='';
}
;"></CommandUIHandler>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
view raw gistfile1.xml hosted with ❤ by GitHub
There are few things to keep in mind.
CustomAction Location="CommandUI.Ribbon" -  tells SharePoint that you are adding controls into the Ribbon.

CommandUIDefination Location="Ribbon.Table.Layout.Summary.Controls._children" - tells SharePoint that the controls need to be added into the "Ribbon.Table" contextual tab under "Layout" tab under "Properties" group.

Command attribute of the control is the place you specify what action you need when a user clicks on the control. Action is specified in the CommandUIHandler element. In my example I am adding the caption element to the table.

QueryCommand attribute of the control is fired when the page is rendered. Action is specified in the CommandUIHandler element. In my example I am fetching the caption element of the table and storing into the textbox.

There are few .js files for Ribbon which are present at 14 Hive layouts folder. These files hold the actions (CommandUIHandler) part of the ribbon.

For Rich Text Editor : "SP.UI.RTE.js"
For Publishing Site Rich Text Editor:  "SP.UI.RTE.Publishing.js"


Hope you find this blog useful.

Cheers,
Isha Jain





Saturday, 23 February 2013

CRUD operations using SPServices

Hello,

These days I am working on client side scripting in SharePoint 2010. I have heavily used SPServices.

SPServices is a JQuery library which interacts with SharePoint web services. It is a script based technology which allows us to talk to SharePoint through its web services.

The syntax is pretty simple, similar like JQuery and other scripting libraries. I have shown below the sample examples to achieve the CRUD operations using SPServices.

Get All the items from the list based on the condition using GetListItems

function ProcessData(xData)
{
  $("#content").html('');
  var ulTag=$('<ul style="list-style:square;"></ul>');  
  $(xData.responseXML).SPFilterNode("z:row").each(function() 
  {  
    var liTag=$("<li></li>").append($(this).attr("ows_Title"))
    ulTag.append(liTag);
  });
  $("#content").append(ulTag);    
}

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLQuery: "<Query><Where><IsNotNull><FieldRef Name='Expires' /></IsNotNull></Where></Query>",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Expires' /><FieldRef Name='Body' /></ViewFields>",
    completefunc: ProcessData
    });   
    
});   

Delete All the items from the list using SPUpdateMultipleListItems

function DeleteAllItemsFromList()
  {
    $.SPServices.SPUpdateMultipleListItems({
    listName: "Announcements",
    CAMLQuery: "<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>",
    batchCmd: "Delete"});
  }

Add item to a list using UpdateListItems

function AddItemsToList(title,content)
  { 
   $.SPServices({
   operation: 'UpdateListItems',
   listName: "Announcements",
   updates: '<Batch OnError="Continue">' + 
             '<Method ID="1" Cmd="New">' +
             '<Field Name="Title">'+ title +'</Field>' +
             '<Field Name="Body">'+ content +'</Field>' +
    '</Method>' +
             '</Batch>',
   completefunc: function(xData, Status)
        {}});  
  }

Update item in a list using UpdateListitems

$function UpdateListItem(itemID)
  { 
    $().SPServices({
    operation: 'UpdateListItems',
    async: false,
    listName: "Announcements", 
    updates: '<Batch OnError="Continue">' + 
             '<Method ID="1" Cmd="Update">' +
      '<Field Name="ID">' + itemID + '</Field>' + //Specifying the item id is very important here
             '<Field Name="Body">'+content+'</Field>' +
      '</Method>' +
             '</Batch>',
    completefunc: function(xData, Status)
    {}
   });  
 }
 
 

 




Cheers,
Isha Jain



Friday, 4 January 2013

PowerShell Basic - Speical Character

Its very difficult to remember every syntax and what it refers to when you are working with myriad of languages.

Below are the useful basic Characters which you need when you work in Power Shell.

1. Single Line comment representation : #

Ex. # This is a single line comment 


2. Multiple Line comments representation: <# #>

Ex. <# This is a Multiple

           Line Comments #>

 

3.Declare a variable: $

Ex. $a=$null

 

4.Item inside for each loop or current pipeline object : $_

Ex. .... | Foreach {Write-Host $_}

 

5.Continue command on next line: `

Ex. Write-Host `

      "Hello World!!"

 

6.Where-Object shortcut : ?

Ex. .... |  1..5 | ? {Write-Host $_ % 2} # Here % represent mod

 

7.Not Shortcut : !

Ex.  $a=$null | if($a) {Write-Host "$a is not null"}

 

8.For each Shortcut: %

Ex. .... | % {Write-Host $_}

 

9. To catch the output of the command and pass it to the another command: |

Ex.  get-process | select-object -first 3


10. Declare Array: @()

Ex. $a= @("one" , "two" , "three")


11. Declare Hash table: @{}

Ex. $a= @{"1" = "one" ; "2" ="two" ; "3" ="three"} 


12. To specify a range: ..

Ex. 1..5 | 


Hope this help

Isha