Complete SharePoint Foundation WCF Form1 Sample

SharePoint 2010

Last modified: February 07, 2011

Applies to: SharePoint Foundation 2010

This topic contains the complete Form1.cs or Form1.vb sample that is used in Walkthrough: Creating and Implementing a Custom WCF Service in 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
Show: