Exemple Form1 WCF SharePoint Foundation complet
Dernière modification : lundi 7 février 2011
S’applique à : SharePoint Foundation 2010
Cette rubrique contient l’exemple Form1.cs ou Form1.vb complet utilisé dans Procédure pas à pas : création et implémentation d’un Service WCF personnalisé dans SharePoint Foundation.
Imports vbProjectTrackerNew.ServiceReference1
Imports System.Net
Imports Microsoft.SharePoint.Client
Imports System.ServiceModel
Imports vbProjectTrackerNew.ServiceReference2
Public Class Form1
Private Shared websiteUrl As String = "http://YourServer/sites/YourSiteCollection/YourSite"
Private context As New TestWebsDataContext(
New Uri(websiteUrl + "/_vti_bin/listdata.svc"))
Private clientContext As New ClientContext(websiteUrl)
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
context.Credentials = CredentialCache.DefaultCredentials
ProjectsBindingSource.DataSource = context.Projects
clientContext.Load(clientContext.Web)
clientContext.ExecuteQuery()
Me.Text = clientContext.Web.Title
End Sub
Private Sub ProjectsBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentChanged
EmployeesBindingSource.DataSource =
From emp In context.Employees _
Where emp.Project.Id = DirectCast(ProjectsBindingSource.Current, ProjectsItem).Id _
Select emp
End Sub
Private Sub ProjectsBindingSource_CurrentItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentItemChanged
context.UpdateObject(ProjectsBindingSource.Current)
End Sub
Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
context.SaveChanges()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim oList As List = clientContext.Web.Lists.GetByTitle("Projects")
oList.Description = String.Format("Star Project of the Week is {0}!!!", DirectCast(ProjectsBindingSource.Current, ProjectsItem).Title)
oList.Update()
clientContext.ExecuteQuery()
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
' Set up proxy.
Dim binding As New BasicHttpBinding()
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm
Dim endpoint As New EndpointAddress(websiteUrl + "/_vti_bin/Revert.svc")
Dim proxy As New RevertClient(binding, endpoint)
proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
' Call web service.
proxy.Revert("Projects", DirectCast(ProjectsBindingSource.Current, ProjectsItem).Id)
' Refresh the UI.
context.MergeOption = System.Data.Services.Client.MergeOption.OverwriteChanges
context.Projects.ToList()
ProjectsBindingSource.ResetCurrentItem()
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SP = Microsoft.SharePoint.Client;
namespace ProjTrack
{
using ServiceReference1;
using System.Net;
using System.ServiceModel;
using ProjTrack.ServiceReference2;
public partial class Form1 : Form
{
private static string websiteUrl= "http://YourServer/sites/YourSiteCollection/YourSite";
TestWebsDataContext context = new TestWebsDataContext(
new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
SP.ClientContext clientContext = new SP.ClientContext("websiteUrl");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
context.Credentials = CredentialCache.DefaultCredentials;
projectsBindingSource.DataSource = context.Projects;
clientContext.Load(clientContext.Web);
clientContext.ExecuteQuery();
this.Text = clientContext.Web.Title;
}
private void projectsBindingSource_CurrentChanged(object sender, EventArgs e)
{
employeesBindingSource.DataSource =
from emp in context.Employees
where emp.Project.Id == ((ProjectsItem)projectsBindingSource.Current).Id
select emp;
}
private void projectsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
context.SaveChanges();
}
private void projectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
{
context.UpdateObject(projectsBindingSource.Current);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
SP.List oList = clientContext.Web.Lists.GetByTitle("Projects");
oList.Description = string.Format("Star Project of the Week is {0}!!!",
((ProjectsItem)projectsBindingSource.Current).Title);
oList.Update();
clientContext.ExecuteQuery();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
// Set up proxy.
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
EndpointAddress endpoint =
new EndpointAddress(websiteUrl + "/_vti_bin/Revert.svc");
RevertClient proxy = new RevertClient(binding, endpoint);
proxy.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
// Call web service.
proxy.Revert("Projects", ((ProjectsItem)projectsBindingSource.Current).Id);
// Refresh the UI.
context.MergeOption = System.Data.Services.Client.MergeOption.OverwriteChanges;
context.Projects.ToList();
projectsBindingSource.ResetCurrentItem();
}
}
}
Voir aussi
Concepts
Services WCF dans SharePoint Foundation 2010