Step1: File->New Project->Silverlight->Silverlight Application->SP_SilverligetApplication1
(Name)->click OK
Pleas make sure check the box "Host the silverlight application in a new web site"->Click OK
Step2: Right clink on SP_SilverlightApplication1->Add service referece->Address as type "http://servername/_vti_bin/People.asmx"->click on "Go" and select an service "People" and give namespace as "PeopleWS"
Step3: Open SP_SilverlightApplication1->MainPage.xml and type following code.
<UserControl x:Class="SP_SilverlightApplication1.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="240" d:DesignWidth="280">
<Grid x:Name="LayoutRoot" Background="White">
<Border BorderThickness="4" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="145" />
<RowDefinition Height="25" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="3" VerticalAlignment="Top">
<TextBlock HorizontalAlignment="Left" Name="textBlock1" Text="Search:" VerticalAlignment="Center" />
<TextBox x:Name="SearchTxt" Width="200" KeyUp="SearchTxt_KeyUp" />
<Button x:Name="SearchBtn" Click="SearchBtn_Click">
<Image Source="/SilverSite;component/Images/search32x32.png" Width="16" Height="16" /> </Button>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Vertical" HorizontalAlignment="Left" Margin="3">
<ListBox x:Name="ResultsLst" Width="265" Height="135" />
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" Margin="3">
<Button x:Name="AddNameBtn" Content="Add ->" Click="AddNameBtn_Click" />
<TextBlock x:Name="UserNameTxt" Width="143" Padding="5" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal" Margin="3" HorizontalAlignment="Right">
<Button x:Name="OKBtn" Content="OK" Width="75" Click="OKBtn_Click" Height="20" Padding="3" />
<Button x:Name="CancelBtn" Content="Cancel" Width="75" Height="20" Padding="3" Click="CancelBtn_Click" />
</StackPanel>
</Grid>
</Border>
</Grid>
</UserControl>
Step4: open SP_SilverlightApplication1->MainPage.xaml.cs and type the following code.
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.Shapes;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
namespace SP_SilverlightApplication1
{
public partial class MainPage : UserControl
{
public string HostName { get; set; }
public string SelectedAccountName { get; set; }
public MainPage HostControl { get; set; }
public enum AddressType
{
Primary,
Secondary
}
public AddressType PickerAddressType { get; set; }
private class PickerEntry
{
public string DisplayName { get; set; }
public string AccountName { get; set; }
public PickerEntry() { }
public PickerEntry(string displayName, string accountName)
{
this.DisplayName = displayName;
this.AccountName = accountName;
}
public override string ToString()
{
return this.DisplayName;
}
}
public MainPage()
{
InitializeComponent();
UserNameTxt.TextDecorations = TextDecorations.Underline;
}
private void OKBtn_Click(object sender, RoutedEventArgs e)
{
//make sure a value was selected
if (string.IsNullOrEmpty(UserNameTxt.Text))
{
MessageBox.Show("You must select a user before clicking OK; if you wish to " +
"cancel this operation then click the Cancel button.", "Select User",
MessageBoxButton.OK);
CancelBtn.Focus();
return;
}
//plug in the values
if (PickerAddressType == AddressType.Primary)
{
HostControl.PrimaryAdmin = SelectedAccountName;
HostControl.PrimaryDisplayName = UserNameTxt.Text;
}
else
{
HostControl.SecondaryAdmin = SelectedAccountName;
HostControl.SecondaryDisplayName = UserNameTxt.Text;
}
CloseDialog();
}
private void CloseDialog()
{
//clear out selections for next time
SearchTxt.Text = string.Empty;
UserNameTxt.Text = string.Empty;
ResultsLst.Items.Clear();
//cast the parent to popup and close; don't set visibility or it
//causes more code the next time you want to open it up
Popup p = (Popup)this.Parent;
p.IsOpen = false;
}
private void SearchBtn_Click(object sender, RoutedEventArgs e)
{
//make sure a search value was entered
if (string.IsNullOrEmpty(SearchTxt.Text))
{
MessageBox.Show("You must enter a search term.", "Missing Search Term",
MessageBoxButton.OK);
SearchTxt.Focus();
return;
}
try
{
//change the cursor to hourglass
this.Cursor = Cursors.Wait;
//the main control has code like this to get the HostName
//get info on the current host
//string curUrl = HtmlPage.Document.DocumentUri.AbsoluteUri.ToString();
//get the host name; note that this assumes the user has rights to the root site
//site collection; that may not be true in your scenario
//Uri curUri = new Uri(curUrl);
//HostName = curUri.Scheme + "://" + curUri.Host + ":" + curUri.Port.ToString();
//set the search request
PeopleWS.PeopleSoapClient ps = new PeopleWS.PeopleSoapClient();
//use the host name property to configure the request against the site in
//which the control is hosted
ps.Endpoint.Address =
new System.ServiceModel.EndpointAddress(HostName + "/_vti_bin/People.asmx");
//create the handler for when the call completes
ps.SearchPrincipalsCompleted +=
new EventHandler<PeopleWS.SearchPrincipalsCompletedEventArgs>(ps_SearchPrincipalsCompleted);
//execute the search
ps.SearchPrincipalsAsync(SearchTxt.Text, 50, PeopleWS.SPPrincipalType.User);
}
catch (Exception ex)
{
//ERROR LOGGING HERE
Debug.WriteLine(ex.Message);
MessageBox.Show("There was a problem executing the search; please try again " +
"later or contact your Help Desk if the problem continues.", "Search Error",
MessageBoxButton.OK);
//reset cursor
this.Cursor = Cursors.Arrow;
}
}
void ps_SearchPrincipalsCompleted(object sender, PeopleWS.SearchPrincipalsCompletedEventArgs e)
{
try
{
if (e.Error != null)
MessageBox.Show("An error was returned: " + e.Error.Message, "Search Error",
MessageBoxButton.OK);
else
{
System.Collections.ObjectModel.ObservableCollection<PeopleWS.PrincipalInfo>
results = e.Result;
//clear the search results listbox
ResultsLst.Items.Clear();
foreach (PeopleWS.PrincipalInfo pi in results)
{
ResultsLst.Items.Add(new PickerEntry(pi.DisplayName, pi.AccountName));
}
}
}
catch (Exception ex)
{
MessageBox.Show("There was an error processing the search results: " + ex.Message,
"Search Error", MessageBoxButton.OK);
}
finally
{
//reset cursor
this.Cursor = Cursors.Arrow;
}
}
private void AddNameBtn_Click(object sender, RoutedEventArgs e)
{
//see if an item is selected
if ((ResultsLst.Items.Count == 0) || (ResultsLst.SelectedItem == null))
{
MessageBox.Show("You must run a search and select a name first.",
"Add User Error", MessageBoxButton.OK);
return;
}
AddPickerEntry();
}
private void AddPickerEntry()
{
//cast the selected name as a PickerEntry
PickerEntry pe = (PickerEntry)ResultsLst.SelectedItem;
UserNameTxt.Text = pe.DisplayName;
SelectedAccountName = pe.AccountName;
}
private void CancelBtn_Click(object sender, RoutedEventArgs e)
{
CloseDialog();
}
private void SearchTxt_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
SearchBtn_Click(sender, new RoutedEventArgs());
}
public string PrimaryAdmin { get; set; }
public string PrimaryDisplayName { get; set; }
public string SecondaryAdmin { get; set; }
public string SecondaryDisplayName { get; set; }
}
}
Step5: Right Click SP_SilverlightApplication->Properties->Build->Output->Output path->"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin\silverlight\"
save all and build the solution
Step6: go to sharepoint application and edit any page and add a silverlight webpart from Categories->Media and Content->Silverlight web part->Add->Url->"_LAYOUTS/ClientBin/silverlight/SP_SilverlightApplication1.xap"->click OK.
Setp7: Test the Application
No comments:
Post a Comment