Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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.

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();
        }




    }
}
Show:
© 2015 Microsoft