Use WCF to reference listdata. SVC to query and update data

Source: Internet
Author: User
ArticleDirectory
    • Preparations
    • Start to use

SharePoint 2010 comes with an out-of-the-box WCF Service-listdata. In this blog, we will learn how to use listdata. SVC step by step.

Preparations
    • Create a website that contains two lists: customer and city. Enter some list items for the list. The customer list should contain a column named City, which is associated with the city list as a query item.
      City list:

      Customer list:
    • Create a console applicationProgram(In fact, it doesn't matter what type of program), add a reference to the WCF Service:

      Note: In this example, I name the reference listdataservice. The listdata service is located in the virtual directory/_ vti_bin/listdata. SVC
Start to use

Now our reference has been created and you need to instantiate it. In this example, I used a Global static member variable to call it in other methods later:

Static   Listdataservice. datacontext of the workgroup website CTX =   Null ;
Static   Void Main ( String [] ARGs)
{
CTX =   New   Listdataservice. datacontex T ( New   U R I ( " Http: // sp2010u/IT/_ vti_bin/listdata. SVC " ));
CTX. Credentials = System. net. credentialcache. defaultcredentials;

Next, you can start querying data:

Static   Void Listcustomerandcitydetails ()
{
VaR customerresults = (From customer In CTX. Customer
Where Customer. City ! =   Null   && Customer. City. Country ! =   Null
Select New
{
Customername = Customer. Customer,
Cityname = Customer. City. City,
Countryname = Customer. City. Country
});
Foreach (VAR customerresult In Customerresults)
{
Console. writeline ( " Customer {0} lives in {2}'s {1} " ,
Customerresult. customername,
Customerresult. cityname,
Customerresult. countryname );
}
}
Static   Void Listcustomers ()
{
VaR customerresults = (From customer In CTX. Customer
Select customer );
Foreach (VAR customerresult In Customerresults)
{
Console. writeline ( " {1}-customer {0} " , Customerresult. Customer, customerresult. ID );
}
}

You can also perform write operations, such as adding data:

Static   Void Addcustomer ( String Cityname, String Customername)
{
VaR customercity = CTX. City. Where (V => V. City = Cityname). firstordefault ();
If (Customercity =   Null )
{
Customercity = Buildnewcity (cityname );
}
Listdataservice. Customer item Client =   New Listdataservice. Customer item ();
Client. City ID = Customercity. ID;
Client. City = Customercity;
Client. Customer = Customername;
CTX. addto client );
CTX. savechanges ();
}

Call the addto Object Name method and savechanges () method to create a list item.

Delete data:

Static   Void Deletecustomer ( Int Idclient)
{
Try
{
CTX. deleteobject (CTX. Customer. Where (C => C. ID = Idclient). firstordefault ());
CTX. savechanges ();
}
Catch (Dataservicequeryexception)
{
Console. writeline ( " The customer cannot be found! " );
}
}

You can call the dataseobject and savechanges methods of dataservicecontext to delete a specified item.

Next we will update the data:

Static   Void Modifycustomersetnewcity ( Int Idclient, String Newcity)
{
VaR targetcustomer = CTX. Customer. Where (C => C. ID = Idclient). firstordefault ();
If (Targetcustomer =   Null )
Throw   New Applicationexception ( " The target customer does not exist! " );
VaR customercity = CTX. City. Where (V => V. City = Newcity). firstordefault ();
If (Customercity =   Null )
{
Customercity = Buildnewcity (newcity );
}
Targetcustomer. City = Customercity;
Targetcustomer. City ID = Customercity. ID;
CTX. updateobject (targetcustomer );
CTX. savechanges ();
}
Private   Static Listdataservice. City item buildnewcity ( String Cityname)
{
Listdataservice. City item newcity =   New Listdataservice. City item ()
{
City = Cityname
};
CTX. addto City (newcity );
CTX. savechanges ();
Return CTX. City. Where (V => V. City = Cityname). firstordefault ();
}

Here you need to call the updateobject method before calling savechanges. You may have noticed that when setting values for a query item, you must specify both the value of the query item and the ID of the query item. In this example, client. City and client. cityid are used. This is not required in splinq...

Execution result of listcustomerandcitydetails:

 

Use Fiddler to view the rest HTTP request corresponding to this query, as shown in:


Click to view the chart

The get part is:

Http: // sp2010u/IT/_ vti_bin/listdata. svc/% E5 % AE % A2 % E6 % 88% B7 ()? $ Filter = (% E5 % 9f % 8e % E5 % b8-% 82% 20Ne % 20 null) % 20and % 20 (% E5 % 9f % 8e % E5 % B8 % 82/% E5 % 9B % BD % E5 % AE % B6 % 20Ne % 20 null) & $ expand = % E5 % 9f % 8e % E5 % B8 % 82 & $ select = % E5 % AE % A2 % E6 % 88% B7, % E5 % 9f % 8e % E5 % B8 % 82/% E5 % 9f % 8e % E5 % B8 % 82, % E5 % 9f % 8e % E5 % B8 % 82/% E5 % 9B % BD % E5 % AE % B6

After the UTF-8 transcoding:

Http: // sp2010u/IT/_ vti_bin/listdata. svc/customer ()? $Filter= (City ne null) and (city/country ne null) & $Expand= City & $Select= Customer, city/city, city/Country

Notes,FilterThe query condition for obtaining customer data is specified,ExpandYou also need to obtain data from the city list. The syntax is very simple.CodeGenerate a similar query URL. For the complete syntax, see msdn.

--- Update ---

The SharePoint 2010 rest service syntax graph on SharePoint in pictures is good.

References

Querying, updating data using a WCF reference to listdata. SVC from a console Program

Tip of the week-using the WCF Service reference to generate rest-like queries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.