Saturday, February 27, 2010

Issue with Visual Studio 2010 RC intellisense

Hi all,


Please refer to this post where in developers are facing problem including me in VS2010 RC's intellisense.  In VS 2010 beta2, there is intellisense for SharePoint 2010. For e.g. if you create a client model such as post in my blog and if you use VS2010 Beta2, there is no problem. If you upgrade to VS2010 RC, I found a small issue in Visual studio's intellisense.


Please see the screen shot where I am developing a client model and want to use <SharePoint:ScriptLink ...>. Surprisingly, there was no intellisense.





But when you type further such as ID, runat attibutes, the intellisense kicks in and you can use intellisense for other attributes.




Looks a bug in VS2010 RC.


Cheers,
--aaroh

Wednesday, February 24, 2010

How to: Enabling a Button on the Ribbon Based on Selection

Hi all, 

Today I read Microsoft SharePoint Team Blog where author showed how to create a button which enables if and only if a single item is selected.Its not a out of the box SharePoint 2010 feature, but it can be achieved very easily using ribbon control and javascript. He has explained very neatly, I will just walk-through. 

Step1: We first create a New Project in Visual Studio 2010  Beta 2/RC >> Choose "SharePoint 2010" as base template >> select "Empty SharePoint Project" >> Name "RibbonBasedSelection" and click OK.
Step2: Right click on "RibbonBasedSelection" >> Add >> New Item >> Under SharePoint 2010 template create a empty element named "RibbonBasedSelection"


Step3. We add a CustomAction which is root of server ribbon control. Later we deal with CommandUIExtension, CommandUIDefinitions where specifically define the location of the command (Location=Ribbon.Documents.New.Controls._children). In "CommandUIHandler" we define the "CommandAction" to show to the users that they can select only one time. We also "EnabledScript" which has a function "singleEnable()". JavaScript uses SharePoint client model to retrieve one time. 


Step4: Build and Deploy the solution.


Step5: Navigate to "Shared Documents" >> click on "Document" tab. We can see the "Single Select". Please note that button is disabled as we have not selected any document. This shows that Ribbon is contextual. 



Now, select the a document. The "Single Select" button is enabled. 

When we click on the "Single Select" button, a javascript pops up showing the "There is one thing selected!" 


If we two documents, then "Single Select" button is disabled again. 

Cheers, 
--aaroh

Download the source here.

Tuesday, February 23, 2010

How to: Create a very basic cutomized Ribbon Control element in SharePoint 2010

Hi all,

SharePoint 2010 sports a brand new fluent user interface and most evident part is the "Ribbon" interface. Ribbon is highly flexible and contextual. When we try to home page, we can view the "Browse" and "Page" tabs.


Developers can leverage the ribbon control and extend to great extent. In this post, I will show a very basic customized ribbon control.

Step1: We first create a New Project in Visual Studio 2010  Beta 2/RC >> Choose "SharePoint 2010" as base template >> select "Empty SharePoint Project" >> Name "NewUIRibbonControl" and click OK.



Step2: Right click on "NewUIRibbonControl" >> Add >> New Item >> Under SharePoint 2010 template create a empty element named "NewUIRibbonControl"



Step3. We add a CustomAction which is root of server ribbon control. ID, RegistrationList, RegistrationID and Location has to be set. Location is appears on the CommandUI.Ribbon.

Later we deal with CommandUIExtension, CommandUIDefinitions where specifically define the location of the commad. (Location=Ribbon.Documents.New.Controls._children)

We create a simple button under "CommandUIDefinition" with its properties such as Alt, Command, LabelText etc. We also define the "CommanUIHandlers" where we define a "CommandAction". The action is simple javascript call to an external web site.



Step4: Deploy the solution. 



Step5: Navigate to "Shared Documents" >> click on "Document" tab. We can see the help button. We can have more advanced scenarios as well.


Cheers,
--aaroh

Download the source. 

Sunday, February 21, 2010

How to: Showing all the lists in a site using SharePoint 2010 with Windows Application (Part 3: .NET)

Hi all,

In my previous post, I have shown how to display all the lists in a site using Client OM java script and using script link to control to register to a page. In this post, I will show the same thing but with Client OM .NET. Moreover, I will use SharePoint client DLLs. I will also filter the lists where title is not null. So, lets get started. 

Step1: We first create a New Project in Visual Studio 2010  Beta 2/RC >> Choose "Visual C#  - Windows" as base template >> select "Windows Forms Application" >> Name "RetriveLists" and click OK. . Please note that choose only .NET framework 3.5.  

Step2: Add References in Solution Explorer and browse to 14\ISAPI folder. 

Step3: Drag two labels "URL" and "Lists", a text box (txtURL), a ListBox (lslLists) and a button "Show Lists". The form should look this. 

Step4:  Double click on the button. Add using statement for Client model. (using ClientOM = Microsoft.SharePoint.Client)
Step5:  In button event, few things have to be noted: 

 private void btnShowLists_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            lstLists.Items.Clear();

            using (ClientOM.ClientContext ctx = new ClientOM.ClientContext(txtURL.Text))
            {
                ClientOM.Web site = ctx.Web;
                ctx.Load(site);
                ctx.Load(site.Lists);
                ctx.Load(site, x => x.Lists.Where(l => l.Title != null));

                ctx.ExecuteQuery();

                foreach(ClientOM.List list in site.Lists)
                {
                    lstLists.Items.Add(list.Title);
                }

            }

a) ClientContext is unmanaged  code. Therefore we have to wrap it with "using" statement. 
b) ClientOM = Microsoft.SharePoint.Client shows intellisense. So, when I typed in "ClientOM". visual studio 2010 showed corresponding properties. For an instance, ClientOM.Web, ctx.Load etc.
c) Load happens in the client and only when "ExceuteQurey" is executed, the form will talk to server. 
d) ctx.Load(site, x => x.Lists.Where(l => l.Title != null)); Getting titles which are null are leveraging by Linq "Lambda" syntax.

This is a less-friendly, but at least consistent and more powerful, way to request data using Linq.  Although a lot of scenarios may start with the "Pretty Query" syntax, in many cases Lambda syntax must be used for more advanced scenarios.
Now, when we execute ctx.Load, we use lambda expression to fetch only titles where title is not null in the top level sites Therefore, we will get the light weight object back.  

Step6: Build and press F5. Key in the site URL and click on "Show Lists". We will get all the lists in site. 


We can get a similar console application sample from MSDN site.

Cheers,
--aaroh

Download source here.

How to: Showing all the lists in a site using SharePoint 2010 (Part 2: Javascript/Ecmascript)


Hi All, 

Using SharePoint 2010 comes up with a new client object model and gives a far better user experience to end users. In this post, I will show a very simple illustration where we can use javascript. What it does is it shows users all the lists in a particular site. The steps are very similar to my last post and I am not showing all the screen-shots here. 

So, lets get started: 

Step1: We first create a New Project in Visual Studio 2010  Beta 2 >> Choose "SharePoint 2010" as base template >> select "Empty SharePoint Project" >> Name "ClientOM" and click OK. . Please note that choose only .NET framework 3.5.  

Step2: Verify the site you want to debug and choose  "Deploy as a farm solution".
Step3: Right click on the "ClientOM" >> Add >> "SharePoint Layouts Mapped Folder" 
 
Step4: Under Layouts folder a new folder will be generated "ClientOM" automatically. Right click on ClientOM folder >> Add >> Create an application page named "AppPage.aspx". 

Step5: We add our JavaScript in "AppPage.aspx". 
There are few things to be noted here: 
a) We have to add the ScriptLink control and it will refer "sp.js" library. This control will register to the page so that we can work with client model. Please note that when you type in <SharePoint , the intellisense kicks in immediately. 

b) We create a javascript function named "execClientOM". It will get current client context, get the reference to the site we are working on, loads all the lists for the current site, and finally execute the query. There would be two delegates for "OnSuccess" and "OnFailure" to display to the users if the operation executed or not. 

c) In "OnSuccess" function, we display the site title and number of lists.  

Step6: Build and Deploy the WSP. 
Step7: We can also debug javascripts in VS2010 as well. Put the breakpoints in javascript functions and press F5. 

Step8: Locate the AppPage.aspx page, http://sp2010/sites/SP2010/_layouts/ClientOM/AppPage.aspx and click on "Exceute Client OM" button.



Cheers, 
--aaroh 

Download the source here.

How to: Create a sample dialog using SharePoint 2010 (Part1: Javascript/Ecmascript)

Hi all, 

SharePoint 2010 sports a brand new fluent user interface. In SharePoint 2007 has commands all over user interface where its Welcome menu, Site Actions menu, List Actions, ECB for list items and end users have tough time to figure out where to find an appropriate command. Page loads happens every time if user have to add a new list column etc.  

SharePoint 2010 comes with some called as Server Ribbon which is contextual and developers can extend it furthermore. Its consistent, streamlined and end user have similar and intuitive office applications interface. Additionally, if use want to work on any site asset such as lists, web part etc., they have to directly go to top of screen and do some necessary actions.

It also improves on status bar and notification area. Developers can write some new places where we use JavaScript without doing a server postback. In short, developers have to learn a new skill set if they are not familiar with JavaScript as SharePoint 2010 heavily uses JavaScript.
They also some with a new Dialog Framework. The main motive of dialog framework is to minimize the postbacks and its uses a modal dialog to interact with user. Technically, the dialog is already there on the page and its hidden. Only when user clicks on an item such as "New Item" in a list, it just pops up as a modal dialog. Modal dialog is used everywhere in SharePoint 2010 such as New Items in lists, document libraries, announcements, editing and deleting items etc. It uses Client Object Model. Microsoft took a different approach and developed a "Client Object Model" and exposed in three different flavors in consistent developer experience across platforms namely:

1)
ECMAScript/JavaScript
2)
.Net.
3) SilverLight. 

In this post I will cover JavaScript module of the client object model. .NET and SilverLight in subsequent posts.

So, lets get started: 

Step1: We first create a New Project in Visual Studio 2010  Beta 2/RC >> Choose "SharePoint 2010" as base template >> select "Empty SharePoint Project" >> Name" DialogSample and click OK. . Please note that choose only .NET framework 3.5.  


Step2: Verify the site you want to debug and choose  "Deploy as a farm solution". 

Step3: Right click on the "DemoSample" >> Add >> "SharePoint Layouts Mapped Folder"



Step4: Under Layouts folder a new folder will be generated "DemoSample" automatically. 
Right click on DemoSample folder >> Add >> Create an application page named "AppPage.aspx". Also, create a html page named "MyDialog.htm"



Step5: We add our JavaScript in "AppPage.aspx". By default, there be 3 content placeholder in AppPage.aspx. 


1) ContentPlaceHolder="PlaceHolderAdditionalPageHead" - allows us to embed head element such as CSS styles and in our case it will be JavaScript file. 
2) ContentPlaceHolder="PlaceHolderMain" - allows us to write in the body area.  In our example we just add button.
3) ContentPlaceHolder="PlaceHolderPageTitleInTitleArea" - allows us to write in title area.

We write our Dialog in "PlaceHolderAdditionalPageHead" as JavaScript. 
Lets view the JavaScript: 


----------------------------------------------------------
<script type="text/javascript">
    function OpenDialog() {
        var options = SP.UI.$create_DialogOptions();
        options.url = "/_layouts/DialogDemo/MyDialog.htm";
        options.width = 400;
        options.height = 300;
        options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
        SP.UI.ModalDialog.showModalDialog(options);
    }

    function CloseCallback(result, target) {
        alert("You called me!");
    }
</script>
-------------------------------------------------------

Visual Studio 2008/2010 supports JavaScript debugging and you can view an excellent tutorial by Joe Stagner. Therefore function, var etc. are automatically available by Visual Studio 2008/2010 IntelliSense. Apparently, SharePoint 2010 has lot of java scripts in layouts folder such as sp.js, sp.debug.js etc and can be found on "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.js, accessible at /_layouts/SP.js. 


SP.JS is unreadable as its Client Model is written by a script named "Script#". The reason for minimized the file is due to increasing the performance. What is mean for developer that they could not leverage this file. But SharePoint 2010 provides a corresponding file named SP.DEBUG.JS. This file has all prototypes and developers can use file to use in their pages and scripts. Therefore, when I wrote the function OpenDialog() as would not be a IntelliSense for client model.* I had to refer to "SP.DEBUG.JS"   


Note: If someone knows how to enable IntelliSense for client model in Visual Studio 2010, please let me know. 


When the user clicks on the "Show Dialog" button, function "OpenDialog()" will be triggered. Now, SP.UI are javascript methods that are made available by javascript that is behind the standard master pages. Variable "options" have properties such as url that be opened by the dialog, width, height etc. 


Another property is "dialogReturnValueCallback" and if someone closed the dialog, we will show an alert. Finally, we pass in all the "options" object to SP.UI.ModalDialog.showModelDialog. 

Step6: Build and Deploy the WSP.




Step7: We can also debug javascripts in VS2010 as well. Put the breakpoints in javascript functions and press F5.


Step8: We have have to clear debugging in IE as well. Tools >> Internet Options >> Advanced>> Clear javascript under Browsing section.



Step9: Locate the AppPage.aspx page, http://sp2010/sites/SP2010/_layouts/DemoSample/AppPage.aspx and click on "Show Dialog" buton.





Step10: Also, when we clcik on "Show Dialog" button, we can debug the javascript code as well.


Step11: We can also set the option.url to a public site too.



Cheers,
--aaroh

Download code here.

Saturday, February 20, 2010

Working with SharePoint 2010 Client Object Model

Hi all, 

There an event conducted by SharePoint user group @ Singapore. This session was to learn the new client object model in the upcoming SharePoint Server 2010. 

The session will cover the following key areas:
• Client Object Application Architecture Overview
• Client Object Model versus Server Object Model.
• Developing Client side applications using .NET Managed Code and ECMA Script.
• Deployment considerations with Client Side Object Model

 Razi Bin Rais (SharePoint Server MVP) conducted the session and facilitated by Steve Sofian. 

 

Why Client Object model??
Microsoft was getting lot of requests from customers, partners for making more web-services so that we can get data out of SharePoint. Therefore, request of more web-services or write your own custom web-services. 

Microsoft took a different approach and developed a "Client Object Model" and exposed in three different flavors in consistent developer experience across platforms namely:
1) .Net.
2) ECMAScript
3) SilverLight.

For e.g. A javascript can run inside the page and is loaded with SharePoint site.  Now, what will happen is that we can circumvent the heavy SOAP-based web services like list service. The best part of client object is strikingly similar to server object model.     

Advantages of client object model: 

>>Site Collections and Sites
>>Lists, List Items, Views, and List Schemas
>>Files and Folders
>>Web, List, and List Item Property Bags
>>Web Parts
>>Security
>>Content Types
>>Site Templates and Site Collection Operations

and limitation of client object model: 

 Equivalent Objects: 

The server objects are very similar to client object model. Programming against server objects in the server context is similar to the new client object model and uses "ClientContext" for all the operations.


Server
(Microsoft
.SharePoint)
.NET Managed
(
Microsoft.SharePoint
.Client)
Silverlight
(
Microsoft.SharePoint
.
Client.Silverlight)
ECMAScript
(SP
.js)
SPContext
ClientContext
ClientContext
ClientContext
SPSite
Site
Site
Site
SPWeb
Web
Web
Web
SPList
List
List
List
SPListItem
ListItem
ListItem
ListItem
SPField
Field
Field
Field

Following are the resources and locations where client object are stroed: 

Sever:
Server – Microsoft.SharePoint – [..]\14\ISAPI

1) Client object model:
1) .NET – Microsoft.SharePoint.Client – [..]\14\ISAPI
2) Silverlight – Microsoft.SharePoint.Client.Silverlight – [..]\14\LAYOUTS\ClientBin
3) ECMAScript – SP.js - [..]\LAYOUTS

As we can notice is that we have 2 different flavors: 

1) .NET Code (.NET managed code and Silverlight)
2) ECMAscript (javascript)

The reason we have different flavors is that read-write properties are different from ECMAscript and .NET code.The syntax are slightly different but the concepts are similar.     

Consider, SPContext obejct is on server side. We can get the same thing on "ClientContext" which is a frame of reference for SharePoint for client model.  Here we pass in the URL for the site you are trying the hit. 

Javascript is different as we CAN NOT pass the URL because if we this, it will cross-site scripting.


Using the Client Object Model: 








 

Following are notes with respect to client model:

>>Manage client OM = .NET / Silverlight
>>All communication goes through the client.svc WCF service
>>Microsoft implemented the client OM by decorating the core SharePoint OM with attributes if it was “client aware”
[ClientCallableType(Name=“Web”,[…]),[…]]
public class SPWeb {}
>>Then a code gen tool generates the client OM assemblies and JavaScript
>>This ensures that there’s fidelity between all the client OM’s and the server equivalent. 

For e.g. 

// get a context

using (ClientContext clientContext = new ClientContext("http://intranet.contoso.com"))

    //Get the Site Collection
    Site site =
clientContext.Site;
   
clientContext.Load(site);
    clientContext.ExecuteQuery();

    //Get the top level site
   Web web = clientContext.Web;
   clientContext.Load(web)
   clientContext.ExecuteQuery();

   // get only the list titles

   clientContext.Load(clientContext.Web, x => x.Lists.Include(l => l.Title).Where(l => l.Title != null));
   clientContext.ExecuteQuery();

   foreach(List list in web.Lists)
    Console.writeline(list.Title);

 }


Explanation: 


//Get the Site Collection
When we get client context, we client model is smart enough and will not get everything from the site collection. At the last line, we get a light weight site collection.  

//Get the top level site
Do the same thing for the top level site.

//Get the only list "Title"
clientContext.Load(clientContext.Web, x => x.Lists.Include(l => l.Title).Where(l => l.Title != null));

Linq "Lambda" syntax.  This is a less-friendly, but at least consistent and more powerful, way to request data using Linq.  Although a lot of scenarios may start with the "Pretty Query" syntax, in many cases Lambda syntax must be used for more advanced scenarios.

Now, when we execute clientContext.Load, we use lambda expression to fetch only titles where title is not null in the top level sites Therefore, we will get the light weight object back.  


2) ECMAScript Client OM

SharePoint 2010 sports ECMAscript (or Javascript) with the client object model. It will give the ability to work on the client after the page is loaded directly with the SharePoint keeping us without any kind of post-back stuff. Although, it does not allow site to another because it will lead to cross-site script and its a big no no in web. 

  Microsoft has developed a javascript library called as "sp.js". ECMAScript Client OM is easily added to a SharePoint ASPX page - reference: 

>> _layouts/sp.js
>> Add this using <SharePoint:ScriptLink>

All compressed and crunched libraries crunched for performances namely  SP.js, SP.Core.js, and SP.Runtime.js. The reason also for users so that they dont have to wait for page to load. 

Use un-crunched *.debug.js by adding <SharePoint:ScriptLink … ScriptMode=“Debug” /> and its only for testing purposes. 


3) SilverLight Client OM 

 In SharePoint 2010, its more easier and powerful to interact with SilverLight applications. There is automatic plumbing in place on the web front end.  There are several benefits: 

>> SilverLight development enabled by Client Object Model.
>> We can use SilverLight in a separate ASPX page or in Web Part.
>> SharePoint 2010 comes with SilverLight web part.

Cheers,
--aaroh

Manage Content and Structure BUG (Exception from HRESULT: 0x81070215)

Hi  All, 

I recently got a bug when I was navigating to "Site Actions" >> Manage Content and Structure and Boom!  . An error was thrown: 
 
BUG: 
-------------------------------------------------------------------------------------------------------------
Exception from HRESULT: 0x81070215 at Microsoft.SharePoint.Library.SPRequestInternalClass.GetViewsSchemaXml(String bstrUrl, String bstrListName, Boolean bFullBlown, ISP2DSafeArrayWriter p2DWriter, Int32& plDefaultViewIndex) at Microsoft.SharePoint.Library.SPRequest.GetViewsSchemaXml(String bstrUrl, String bstrListName, Boolean bFullBlown, ISP2DSafeArrayWriter p2DWriter, Int32& plDefaultViewIndex)
-----------------------------------------------------------------------------------------------------------

A very nasty error. But the error code (HRESULT: 0x81070215) reminded of the error which I had seen before. Its was in my blog

It happens when open a list, either the list is has not properly been deleted and is in a corrupted state. I have revisited my site and found that one of my custom list named "Annotations" was in corrupted state. Or may be I had deleted the solution but not deactivated on the site feature. 

FIX: 

Although the help message displays information about using the forcedeleteweb operation, you have to use forcedeletelist instead. The syntax is:
stsadm.exe -o forcedeletelist -url <url>
For e.g. if the list called as "Annotations" has corrupted just fire up this command:

stsadm -o forcedeletelist -url http://server/apac/lists/annotations

For more info:
http://bloggingabout.net/blogs/harold/archive/2009/01/10/deleting-a-broken-sharepoint-list-using-stsadm.aspx

http://technet.microsoft.com/en-us/library/cc262609.aspx

Just fire the stsadm command and activate the feature.
Now, again go to "Site Actions" >> "Manage Content and Structure". 



It will work fine. 


Cheers, 
--aaroh

Access denied publishing Feature

Hi all, 

Recently I was working on SharePoint 2007 site and created a site collection in Central Administration. I followed these steps: 

1) Created a new site collection.
2) Keyed in the "Title" and "Description".
3) Chose "Team Site" as the Site Template.

Site Collection was created.

4) Navigated to "Site Actions" >> Site Collection Features >> Activated the "Office SharePoint Server Publishing Infrastructure"

Surprisingly, I got an error:

BUG:
The office SharePoint Server Publishing infrastructure feature must be activated at the site collection level..

After some research, I found that Microsoft suggest to use "Server Farm Account" and must use as the AppPool account for Central Administration. In addition, i referred to one the blogs to fix this issue

1. Start >> Run >>  inetmgr 
2. Find the web site our web application. 
3. Right click >> Properties and choose the ‘Home Directory’ tab.
4. Change the AppPool to be the same as Central Administration.
5. IISRESET
6. Activate the Publishing Infrastructure feature on your site.
7. Change to AppPool back to the original.
8. IISRESET

Theoretically, it should work. It worked for me as well. 

Cheers.
--Aaroh

Saturday, February 13, 2010

Getting started with SharePoint 2010

Hi All, 

Last week I bought a new PC only for SharePoint 2010 development. It has a pretty high-end configuration specifications: Intel i7-920 processor, Windows 7 home premium (64 bit)10 GB DDR 3 RAM, 1 TB HDD, Nvidia GeForce GT230 Graphics Card (1.5GB Dedicated RAM)

The PC has incredible performance. I also downloaded VMware player and got a SharePoint VM from one of my colleagues. Microsoft has given a lot of free sources for developer community and it could be find here. It has lot of hands-on labs, solutions, lot of videos, slide-shows etc. 

Today, I watched Ted Pattison's SharePoint 2010 Development Primer and created a new SharePoint 2010 solution. Its just a simple console application. There are few things to be noted here: 

  1. SharePoint 2010 is 64 bit. Therefore developers has to take note of that.
  2. SharePoint 2010 is based on .NET framework 3.5. That's why if use .NET FX 4.0 you are bound to get an error. 
Ted Pattison created a console application where he a SPSite and SPWeb for a SharePoint 2010 site and displays all the lists in the web. But he got an error "File not found." So, he went to properties, navigated to "Build" tab and in the "Platform target" he switched to "Any CPU". I did exact steps. For him it worked perfectly well but I got an error.  

"The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)"

I don't know what was wrong. I again went to solution properties,  navigated to "Application" tab and found that "Target framework" was pointing to ".NET Framework 4 client profile". Obviously, SharePoint 2010 does not support .Net FX 4.0. I switched to .NET framework 3.5. 

Voila!

It worked beautifully!  
My SharePoint 2010 journey has just started! 

Cheers, 
--aaroh

Low Code Reimagined with AI + Copilot Pitch Deck - Copy Copilot day (Virtual) - 2023

 Hi All,  I presneded a session at Pune UG on Low Code Reimagined with AI + Copilot Pitch Deck.  Video is at this address  https://www.youtu...