Complete SharePoint Foundation WCF Form1 Sample

Expand
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

Complete SharePoint Foundation WCF Form1 Sample

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




    }
}

Date

Description

Reason

May 2010

Initial publication

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD
Bug with MultipleBaseAddressBasicHttpBindingServiceHostFactory when not hosted at the Root
There appears to be a bug in MultipleBaseAddressBasicHttpBindingServiceHostFactory that means that the endpoints generated by that factory are only correct if the site is hosted in the root. e.g. If your site collection is hosted in say /sites/myportal and you deploy your WCF Service there, then the endpoint will be:
http://servername/mycompanyname/_vti_bin/myportal/CommonService.svc  when it should really be
http://servername/mycompanyname/sites/myportal/_vti_bin/CommonService.svc 

When this happens, your service will work but the SPContext will be null.

For more information and workarounds, see http://ddkonline.blogspot.com.au/2012/02/sharepoint-2010-when-hosting-wcf.html
2/13/2012