Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.
Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Thursday, March 28, 2013

System.NullReference Exception in Visual Studio 2010 Silverlight Designer: Resolved



  1. First remove the SilverLight from control panel in windows7.
  2. Now download the files of SilverLight from here.  The name of the files will be silverLight_Downloads.rar and Silverlight_Tools.rar.
  3. Now install the Silverlight_Developer.exe version4 given in the silverLight_Downloads.rar.
  4. Now restart your browser.
  5. Create a SilverLight web application. Now you can see the System.NullReference exception is gone.

Monday, December 05, 2011

Get files from document library Silverlight client object model

Get files from document library Silverlight client object model
Here is a detailed code snippet for creating a silverlight applictaion that will display all the files\documents from a document library in a webpart using silverlight Client Object model.
To begin with
1. In Visual Studio 2010 create a new Blank Solution. Select .NET Framework 3.5.
2. Next, from the File menu open click on Add -> New Project.
3. In the New Project dialog box, expand the Installed Templates left hand menu to Visual C# -> Silverlight, and choose the Silverlight Application project type in the project type list in the middle section of the screen. Make sure that you have .NET Framework 3.5 selected. Click Ok to continue on Silverlight Application dialog box.
4. Next, add the required references to the Silverlight client object model. To do this browse to “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin” and select Microsoft.SharePoint.ClientSilverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll
5. In the solution explorer open the App.xaml.cs and add the following references to the top of the page.
using Microsoft.SharePoint.Client;
using System.Threading;
6. Next in the same file App.xaml.cs, under Applictaion_Startup method add the below to get the refrence to the current sharepoint site and to ensure that we are in sync with the current SharePoint site.
ApplicationContext.Init(e.InitParams, Synchronizationcontext.Current);
7. Next, lets open up our MainPage.xaml and Add two StackPanels under the existing grid.
<StackPanel Orientation=”Vertical” VerticalAlignment=”Top”>
<StackPanel Name=”mypanel” />
</StackPanel>
8. Now, Open MainPage.xaml.cs and add the following using statements to the top of the file:
using Microsoft.SharePoint.Client;
9. Next, add the following code to get all the files from your SharePoint document library in the silverlight webpart.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
ClientContext clientCtx;
Microsoft.SharePoint.Client.List docs;
int FileCounter= 0;
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
clientCtx = new ClientContext(ApplicationContext.Current.Url);
clientCtx.Load(clientCtx.Web);
clientCtx.Load(clientCtx.Web.Lists);
docs = clientCtx.Web.Lists.GetByTitle(“My Documents”);
clientCtx.Load(docs);
clientCtx.Load(docs.Rootfolder);
clientCtx.Load(docs.Rootfolder.Files);
clientCtx.ExecuteQueryAsync(loadSiteData, null); // We will move to loadSiteData method on success
}
void loadSiteData(Object sender, ClientRequestSucceededEventArgs e)
{
//This method starts on a background thread, but we need to work on UI to display our files Therefore we call Dispatcher.BeginInvoke() to perform the UI updating on the main thread
Dispatcher.BeginInvoke(addFiles);
}
void addFiles()
{
foreach (File doc in docs.Rootfolder.Files)
{
clientCtx.Load(doc);
clientCtx.ExecuteQueryAsync(addFiletoUI, null); // We will move to loadSiteData method on success
}
}
void addFiletoUI(Object sender, ClientRequestSucceededEventArgs e)
{
Dispatcher.BeginInvoke(addFile);
}
void addFile()
{
string filename = docs.Rootfolder.Files[FileCounter].Name;
string fileUrl = docs.Rootfolder.Files[FileCounter].ServerRelativeUrl;
HyperlinkButton fLink= new HyperlinkButton();
fLink.Content = filename;
fLink.NavigateUri = new Uri(fileUrl , UriKind.Relative);
mypanel.Children.Add(fLink);
FileCounter++;
}
}
10. Build and deploy the webpart.
11. Goto SharePoint site and add a silverlight webpart. Point the webpart to “”/_layouts/ClientBin/YourSilverlightApp_Name.xap” and your Done! yeh!

How do you access ECMAScript object model API’s

The ECMAScript library is available in a number of JS files in the LAYOUTS folder. The main file among number of .js files is SP.js. When you include this file in the APSX page using a ScriptLink control, all other required JS files are loaded automatically. By linking SP.js to your page, the SP namespace gets registered. SP is the SharePoint namespace that contains all objects. For debugging purposes every js file also has a ‘debug’ equivalent in the same folder.