Saturday, August 11, 2012

WCF with sharepoint 2010 service

Try below code, it works perfectly fine:-

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Linq;
using Microsoft.SharePoint.Linq;
using WCFService.ListWCFService;
namespace WCFService.WP_WCFService
{
    public partial class WP_WCFServiceUserControl : UserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                System.Uri uri = new Uri("http://localhost:16178/_vti_bin/ListData.svc");
               SrgDataContext dc = new SrgDataContext(uri);
               dc.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    var query = from dev in dc.Announcements
                                select new
                                {
                                    Title = dev.Title,
                                    Body = dev.Body,
                                    ExpiryDate = dev.Expires,
                                    Path = dev.Path
                                };
                    gridAnnouncementList.DataSource = query;
                    gridAnnouncementList.DataBind();
             
                //dc.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           

                try
                {
                    System.Uri uri = new Uri("http://localhost:16178/_vti_bin/ListData.svc");
                    WCFService.ListWCFService.SrgDataContext dc = new WCFService.ListWCFService.SrgDataContext(uri);
                    dc.Credentials = System.Net.CredentialCache.DefaultCredentials;
                   
                   dc.AddToAnnouncements(
                            new AnnouncementsItem
                            {
                                Title = txtTitle.Text,
                                Body = txtBody.Text
                            });

                    //dc.Announcements.InsertOnSubmit(newAnnouncement);
                    //dc.c();
                   dc.SaveChanges();
                   // dc.Dispose();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
          
      
        }
    }
}


No comments:

Post a Comment

SharePoint document metadata not updating

I faced a weird issue today, Metadata for document which has lookup column was not updating even after saving the item. There was no erro...