IT tutorials
 
Windows
 

Windows Azure : Integrating BLOB Storage and SharePoint (part 3) - Consuming BLOB Storage Data with Silverlight

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
9/29/2011 5:13:32 PM

5. Consuming BLOB Storage Data with Silverlight

Because Silverlight is effectively being hosted within a Silverlight Web Part, the integration with SharePoint is light. However, you can use what is called the SharePoint client object model to interact with SharePoint on a deeper level. The SharePoint client object model is a remote API that enables you to manage data going into and coming out of SharePoint, among other things. By using this API, you can build JavaScript, .NET Framework, and Silverlight applications that integrate with SharePoint.


Note:

More Info For more information on the SharePoint client object model, see “What’s New: Client Object Model” at http://msdn.microsoft.com/en-us/library/ee535231.aspx.


5.1. Consume BLOB Storage Data within a Silverlight Application
  1. Open the Visual Studio solution , and click Add | New Project.

  2. Select Silverlight as your project template, provide a name for the project (such as Silverlight_Image_Viewer), and click OK. Clear the Host The Silverlight Application In A New Or Existing Web Site In The Existing Solution check box, and click OK.

    In this example, you’ll create a UI that includes several controls. The following table provides an overview of the control types and variable names for those controls. Note that some of these are user controls and others are more for UI formatting (such as the Canvas and Rectangle controls).

    Control TypeVariable Name
    CanvasazureCanvas
    RectanglerectangleForSharePointCtrls
    CanvasazureImageCanvas
    LabellblAddToSharePoint
    LabellblName
    LabellblLink
    TextboxtxtbxName
    TextboxtxtbxLink
    LabellblCoolness
    ComboboxcmbobxPriority
    LabellblNotes
    TextboxtxtbxNotes
    LabellblImages
    ButtonbtnSaveToSharePoint
    ButtonbtnClearFields
    ButtonbtnLoadImage
    ComboboxcmbobxImages
    LabellblMetadata
    LabellblImageInfo
    LabellblAzureImage

  3. In the MainPage.xaml file, amend the UI code as shown in bold in the following code listing:

    <UserControl x:Class="Silverlight_Media_Player.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="400" d:DesignWidth="800" xmlns:sdk=
    "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    <Canvas Width="800" x:Name="azureCanvas">
    <Rectangle Height="400" Name="rectangleForSharePointCtrls"
    Stroke="Black" StrokeThickness=".5" Width="796" >
    <Rectangle.Fill>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="Black" Offset="0"/>
    <GradientStop Color="White" Offset="1"/>
    </LinearGradientBrush>
    </Rectangle.Fill>
    </Rectangle>
    <Canvas Width="387" Height="299" x:Name="azureImageCanvas"
    Canvas.Left="17" Canvas.Top="66" Background="Black"/>
    <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
    BorderBrush="Black" BorderThickness="2">
    <Border.Background>
    <VideoBrush SourceName="imageViewerCtrl" Stretch="UniformToFill"/>
    </Border.Background>
    </Border>
    <sdk:Label Canvas.Left="440" FontSize="14" FontWeight="Bold"
    Content="Archive with Notes" Foreground="White" Canvas.Top="22"
    Height="28" Name="lblAddToSharePoint" Width="165" >
    </sdk:Label>
    <sdk:Label Canvas.Left="440" Foreground="White" Content="Name:"
    Canvas.Top="133" Height="28" Name="lblName" Width="50" />
    <sdk:Label Canvas.Left="440" Foreground="White" Content="Link:"
    Canvas.Top="167" Height="28" Name="lblLink" Width="50" />
    <TextBox IsEnabled="False" Canvas.Left="514" Canvas.Top="133" Height="22"
    Name="txtbxName" Width="274" />
    <TextBox IsEnabled="False" Canvas.Left="514" Canvas.Top="173" Height="22"
    Name="txtbxLink" Width="274" />
    <sdk:Label Canvas.Left="440" Content="Coolness:" Foreground="White"
    Canvas.Top="244" Height="28" Name="lblCoolness" Width="68" />
    <ComboBox Canvas.Left="514" Canvas.Top="244" Height="22"
    Name="cmbobxPriority" Width="120" >
    <ComboBoxItem Content="1"></ComboBoxItem>
    <ComboBoxItem Content="2"></ComboBoxItem>
    <ComboBoxItem Content="3"></ComboBoxItem>
    <ComboBoxItem Content="4"></ComboBoxItem>
    <ComboBoxItem Content="5"></ComboBoxItem>
    </ComboBox>
    <sdk:Label Canvas.Left="440" Content="Notes:" Foreground="White"
    Canvas.Top="293" Height="28" Name="lblNotes" Width="50" />
    <Button Canvas.Left="569" Canvas.Top="355" Content="Save" Height="22"
    Name="btnSaveToSharePoint" Width="98"
    Click="btnSaveToSharePoint_Click" />
    <Button Canvas.Left="690" Canvas.Top="355" Content="Clear" Height="22"
    Name="btnClearFields" Width="98" Click="btnClearFields_Click" />
    <TextBox Canvas.Left="514" Canvas.Top="286" Height="48" Name="txtbxNotes"
    Width="274" TextWrapping="Wrap"/>
    <sdk:Label Canvas.Left="440" Foreground="White" Content="Images:"
    Canvas.Top="83" Height="28" Name="lblImages" Width="50" />
    <ComboBox SelectionChanged="cmbobxImages_SelectionChanged"
    Canvas.Left="514" Canvas.Top="90" Height="22" Name="cmbobxImages"
    Width="275" />
    <Button Canvas.Left="440" Canvas.Top="355" Content="Load Images"
    Height="22" Name="btnLoadImage" Width="98" Click="btnLoadImage_Click" />
    <sdk:Label FontWeight="Bold" Foreground="White" Canvas.Left="419"
    Canvas.Top="204" Height="29" Name="lblMetadata" Width="160"
    Content="Additional Information" />
    <sdk:Label Canvas.Left="419" Canvas.Top="51" Content="Image Information"
    FontWeight="Bold" Foreground="White" Height="29" Name="lblImageInfo"
    Width="160" />
    <sdk:Label Canvas.Left="17" Canvas.Top="22" Content="Image from Azure"
    FontSize="14" FontWeight="Bold" Foreground="White" Height="28"
    Name="lblAzureImage" Width="165" />
    </Canvas>
    </UserControl>


    When you’ve completed the code, your Silverlight application UI should look similar to the one shown in the following graphic.



  4. Add the WCF service reference you created in the previous exercise to this project by right-clicking Service Reference and selecting Add Service Reference.

  5. Add the service URI (for example, http://localhost:4966/GetAzureStorageData.svc) in the Address field, and click Go. When the service resolves, provide a namespace (such as AzureImagesWCFService) and click OK.

  6. Right-click the MainPage.xaml file in the Solution Explorer, and select View Code. Amend the code in the Silverlight code-behind as shown in the following code listing:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Silverlight_Media_Player.AzureImagesWCFService;
    using System.Xml;
    using System.Xml.Linq;
    using System.Linq.Expressions;
    using Microsoft.SharePoint.Client;


    namespace Silverlight_Media_Player
    {
    public partial class MainPage : UserControl
    {
    List<ImageInfo> listOfImages = new List<ImageInfo>();
    GetAzureStorageDataClient myServiceProxy = new GetAzureStorageDataClient();

    string imageName = "";
    string imageURI = "";

    string azureImageName = "";
    string azureImageURI = "";
    string azureImageCoolness = "";
    string azureImageNotes = "";
    string mySPSite = "";

    public MainPage()
    {
    InitializeComponent();
    }

    private void btnLoadImage_Click(object sender, RoutedEventArgs e)
    {
    myServiceProxy.GetDataCompleted += new
    EventHandler<GetDataCompletedEventArgs>(
    myServiceProxy_GetDataCompleted);
    myServiceProxy.GetDataAsync();
    }

    void myServiceProxy_GetDataCompleted(object sender,
    GetDataCompletedEventArgs e)
    {
    cmbobxImages.Items.Clear();

    var returnBlobData = e.Result;
    XDocument blobXMLData = XDocument.Parse(returnBlobData);

    foreach (var p in blobXMLData.Descendants("Blob"))
    {
    ImageInfo tempImage = new ImageInfo();
    tempImage.imageName = p.Element("Name").Value;
    tempImage.imageURL = p.Element("Url").Value;
    listOfImages.Add(tempImage);

    cmbobxImages.Items.Add(p.Element("Name").Value);

    imageName = p.Element("Name").Value;
    imageURI = p.Element("Url").Value;
    };
    }

    private void cmbobxImages_SelectionChanged(
    object sender, SelectionChangedEventArgs e)
    {
    UpdateUIControlProperties();

    Image azureImage = new Image();
    azureImage.Width = 387;
    azureImage.Height = 299;
    azureImage.Stretch = Stretch.UniformToFill;
    Uri azureURI = new Uri(imageURI, UriKind.Absolute);
    ImageSource azureImageSource = new
    System.Windows.Media.Imaging.BitmapImage(azureURI);
    azureImage.SetValue(Image.SourceProperty, azureImageSource);

    azureImageCanvas.Children.Clear();
    azureImageCanvas.Children.Add(azureImage);
    }

    private void UpdateUIControlProperties()
    {
    string fileNameFilter = "";
    fileNameFilter = cmbobxImages.SelectedValue.ToString();

    var query = from filteredImage in listOfImages
    where filteredImage.imageName == fileNameFilter
    select filteredImage;

    foreach (var item in query)
    {
    imageName = item.imageName;
    imageURI = item.imageURL;
    }

    imageName = imageName.Substring(0, imageName.LastIndexOf("."));
    txtbxName.Text = imageName;
    txtbxLink.Text = imageURI.ToString();
    }

    private void btnClearFields_Click(object sender, RoutedEventArgs e)
    {
    txtbxNotes.Text = "";
    }

    private void btnSaveToSharePoint_Click(object sender, RoutedEventArgs e)
    {
    ConnectToSharePoint();
    }

    private void ConnectToSharePoint()
    {
    azureImageName = txtbxName.Text;
    azureImageURI = txtbxLink.Text;
    azureImageCoolness = cmbobxPriority.SelectionBoxItem.ToString();
    azureImageNotes = txtbxNotes.Text;
    mySPSite = "http://blueyonderdemo";
    ClientOM.ClientContext mySPContext = new ClientContext(mySPSite);
    ClientOM.List azureImageList = mySPContext.Web.Lists.GetByTitle(
    "Azure Images");
    mySPContext.Load(mySPContext.Web);
    mySPContext.Load(azureImageList);

    ListItemCreationInformation newImageRecord = new
    ListItemCreationInformation();
    ClientOM.ListItem newImage = azureImageList.AddItem(newImageRecord);

    newImage["Title"] = azureImageName;
    newImage["Link"] = azureImageURI;
    newImage["Coolness"] = azureImageCoolness;
    newImage["Notes"] = azureImageNotes;
    newImage.Update();

    mySPContext.ExecuteQueryAsync(OnItemAddedSucceeded, OnItemAddedFailed);
    }

    private void OnItemAddedSucceeded(Object sender,
    ClientOM.ClientRequestSucceededEventArgs args)
    {
    MessageBox.Show("SharePoint List Updated!");
    }

    private void OnItemAddedFailed(Object sender,
    ClientOM.ClientRequestFailedEventArgs args)
    {
    Dispatcher.BeginInvoke(() => ShowErrorMessages(args));
    }

    private void ShowErrorMessages(ClientRequestFailedEventArgs args)
    {
    MessageBox.Show(args.Message + " " + args.ErrorValue);
    }
    }
    }


    In this large chunk of code, you’re doing three things: retrieving the information you need from BLOB storage (that is, the name and image URI), displaying the image from BLOB storage in the Silverlight image viewer, and adding user-entered information into a SharePoint list by using the SharePoint client object model.

    To retrieve information from BLOB storage, you’re implementing the WCF service you deployed to your local IIS. You could instead deploy this service to Windows Azure as a web role, in which case you could use this service in SharePoint Online as well as SharePoint Server. You can see that the core code is implemented in the myServiceProxy_GetDataCompleted event. Here you can see that you’re representing the return XML data as a var called returnBlobdata and then you’re using an XDocument object to parse the return data. You might have a variation on how you serialize and deserialize data, so you don’t necessarily have to use XDocument. You’re then cycling through the return data and adding the information to the combo box (cmbobxImages):

          ...

    void myServiceProxy_GetDataCompleted(object sender,
    GetDataCompletedEventArgs e)
    {
    cmbobxImages.Items.Clear();

    var returnBlobData = e.Result;
    XDocument blobXMLData = XDocument.Parse(returnBlobData);

    foreach (var p in blobXMLData.Descendants("Blob"))
    {
    ImageInfo tempImage = new ImageInfo();
    tempImage.imageName = p.Element("Name").Value;
    tempImage.imageURL = p.Element("Url").Value;
    listOfImages.Add(tempImage);

    cmbobxImages.Items.Add(p.Element("Name").Value);

    imageName = p.Element("Name").Value;
    imageURI = p.Element("Url").Value;
    };
    }

    ...

    After the data has been added to the combo box control, you can select an image to be displayed in the Image control (azureImage). The event that manages this display and redisplay of images is the cmbobxImages_SelectionChanged event. To properly display the metadata, this code uses a helper function (UpdateUIControlProperties) to get the right name and URI of the image in the appropriate textboxes (which are disabled for user entry). The cmbobxImages_SelectionChanged event then creates a new instance of an Image object, sets the properties of the image, and then sets the source of the image to be displayed in the Silverlight control:

          ...

    private void cmbobxImages_SelectionChanged(
    object sender, SelectionChangedEventArgs e)
    {
    UpdateUIControlProperties();

    Image azureImage = new Image();
    azureImage.Width = 387;
    azureImage.Height = 299;
    azureImage.Stretch = Stretch.UniformToFill;
    Uri azureURI = new Uri(imageURI, UriKind.Absolute);
    ImageSource azureImageSource = new
    System.Windows.Media.Imaging.BitmapImage(azureURI);
    azureImage.SetValue(Image.SourceProperty, azureImageSource);

    azureImageCanvas.Children.Clear();
    azureImageCanvas.Children.Add(azureImage);
    }

    private void UpdateUIControlProperties()
    {
    string fileNameFilter = "";
    fileNameFilter = cmbobxImages.SelectedValue.ToString();

    var query = from filteredImage in listOfImages
    where filteredImage.imageName == fileNameFilter
    select filteredImage;

    foreach (var item in query)
    {
    imageName = item.imageName;
    imageURI = item.imageURL;
    }

    imageName = imageName.Substring(0, imageName.LastIndexOf("."));
    txtbxName.Text = imageName;
    txtbxLink.Text = imageURI.ToString();
    }

    ...


    Although more illustrative than functional, the third part of the application enables you to add data to a SharePoint list based on user-entered text. This is handled within the ConnectToSharePoint method, which not only connects to SharePoint but also takes information from the user controls within the Silverlight application and then adds the information in those controls to a list called Azure Images. The key point here is that you can use the SharePoint client object model to inject information programmatically into SharePoint—thus creating a connection with your BLOB storage metadata. The use of the SharePoint client object model can equally apply to SharePoint Server and SharePoint Online. The key code for this is the creation of the context by using the ClientContext object, which establishes a connection with your SharePoint site. You set several properties for the ClientContext object, such as the site to which you’re connecting, the list with which you want to interact, and so on. You also must explicitly create the list item and set its properties, which is reflected in the following code through the newImage object (which maps back to properties of the UI controls) and the call to the Update method. You could, of course, optimize the code to map directly to the user control properties as opposed to having a string variable intermediary. The final event that is called is the ExecuteQueryAsync method, which takes all of the code within the ConnectToSharePoint method and batch processes it against the SharePoint server. The result is a new line-item entry in the Azure Images list:

          ...

    private void ConnectToSharePoint()
    {
    azureImageName = txtbxName.Text;
    azureImageURI = txtbxLink.Text;
    azureImageCoolness = cmbobxPriority.SelectionBoxItem.ToString();
    azureImageNotes = txtbxNotes.Text;
    mySPSite = "http://blueyonderdemo";

    ClientOM.ClientContext mySPContext = new ClientContext(mySPSite);
    ClientOM.List azureImageList = mySPContext.Web.Lists.GetByTitle(
    "Azure Images");
    mySPContext.Load(mySPContext.Web);
    mySPContext.Load(azureImageList);

    ListItemCreationInformation newImageRecord = new
    ListItemCreationInformation();
    ClientOM.ListItem newImage = azureImageList.AddItem(newImageRecord);

    newImage["Title"] = azureImageName;
    newImage["Link"] = azureImageURI;
    newImage["Coolness"] = azureImageCoolness;
    newImage["Notes"] = azureImageNotes;
    newImage.Update();

    mySPContext.ExecuteQueryAsync(OnItemAddedSucceeded, OnItemAddedFailed);
    }

    ...


  7. Now that the Silverlight application is complete, press F6 on your keyboard to build the project. Then right-click the project and select Open In Windows Explorer.

  8. Browse to the ~/bin/debug folder and copy the folder path to your Clipboard. You’ll use this link to upload the XAP file to a document library.

  9. Open your SharePoint site and navigate back to the XAPS document library .

  10. Select Add Document.

  11. Click Browse and copy the directory path to your Silverlight XAP file in the Choose File To Upload dialog box. Select the XAP (for example, Silverlight_Media_Player.xap) and click Open. Complete the upload process by clicking OK in the Upload Document dialog box.

  12. In the XAPS document library, right-click the newly added XAP file and select Copy Shortcut to copy a link to the Silverlight application to your Clipboard.

  13. Navigate to a Web Part page in the SharePoint site and click Site Actions | Edit Page.

  14. Click the Insert tab, and select Web Part.

  15. Navigate to the Media And Content category and select Silverlight Web Part.

  16. Click Add, copy the shortcut to the Silverlight application in the URL field, and click OK. (Note that you might need to resize the Web Part.)

At this point, you’ve added a Silverlight application to SharePoint that not only loads the images from Windows Azure but also enables you to push data into SharePoint from the Silverlight application by using the client object model. For example, in Figure 2 you can see that the Silverlight Web Part is co-located to the ASP.NET application deployed to Windows Azure (one Web Part is Silverlight-based and the other is IFRAME-based). So you can now add an image from within SharePoint (the images are stored in Windows Azure BLOB storage) and then view and amend that image with additional metadata that can then be inserted into a SharePoint list.

Figure 2. The Silverlight Web Part and Windows Azure application using BLOB storage in SharePoint.


You click the Load Images button to call the WCF service and load information from Windows Azure into the Silverlight application. After you’ve loaded the images, you can filter by using the Images combo box and then select a Coolness factor, add some notes, and click Save to push information into SharePoint. When the information is inserted into SharePoint, it will look similar to what is shown in Figure 3.

Figure 3. Adding information from BLOB storage to SharePoint.


The image viewer scenario was straightforward, yes, but you can apply the principles illustrated in the exercise in many different ways. For example, the Silverlight application could act as a video player, and you could use the SharePoint list as your personal learning portfolio—adding information and links to your learning list for specific learning areas. You could add documents to BLOB storage equally well.

 
Others
 
- Windows Azure : Integrating BLOB Storage and SharePoint (part 2) - Deploying the Application & Integrating the Application with SharePoint
- Windows Azure : Integrating BLOB Storage and SharePoint (part 1) - Creating the Application
- Overview of Windows Azure BLOB Storage
- Windows 7 : Managing Network Access
- Windows 7 : Managing File and Folder Security (part 2) - Design Goals for Access Control & Determining Effective Permissions
- Windows 7 : Managing File and Folder Security (part 1) - Folder Options, Securing Access to Files and Folders
- Automating Administrative Tasks with Windows Server 2008 R2 : Learn PowerShell Integrated Scripting Environment (ISE)
- Automating Administrative Tasks with Windows Server 2008 R2 : Use PowerShell Remoting
- New Group Policy Features in Windows Server 2008
- New Group Policy Features in Windows Vista
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us