Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

We have an existing ASHX page in our 2010 farm that users hit to pull in some RSS data. I'm wanting to do some tracking on the usage of this page. I don't want to IIS log parse for this so my thought was to track it in a separate custom list. Whenever a user hits the page, check if they exist in the list. If not, add a record. This part I have working no problem using REST.

My issue arises on how to update the existing list item with updated information. I can successfully get back my list item and craft the update I want but I'm not exactly sure how to do the update.

Do I need a digest to perform the update or will having the eTag work?

Note that this in on an ahsx page that is requested with GET requests so I simply can't do the normal item.Update() stuff and I'm not wildly fond of constantly switching allowunsafeupdates back and forth to log the data. I'm currently trying to do an update like this:

 public void UpdateListItem(int itemId, string user)
        {
            var uri = string.Format("http://localhost/_vti_bin/ListData.svc/RSSData?$filterId eq ({0})", itemId);
            var title = string.Format("{0} {1}", user, System.DateTime.Now);
            var data = "{'__metadata': { 'type': 'SP.Data.RSSDataItem' }, 'Title': '" + title + "'}";

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            request.Method = "POST";
            request.Accept = "application/json; odata=verbose";
            request.ContentType = "application/json; odata=verbose";
            request.Headers.Add("X-HTTP-Method", "MERGE");
            request.Headers.Add("IF-MATCH", "*");

            request.ContentLength = data.Length;
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(data);
            writer.Flush();

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string result = reader.ReadToEnd();
                }
            }
        }
share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

In order to perform an Update operation, the following issues/typos should be fixed in the specified example

1) REST endpoint

There is a typo in the specified request /_vti_bin/ListData.svc/RSSData?$filterId eq <ItemID>

The proper syntax for request is

/_vti_bin/ListData.svc/RSSData?$filter=Id eq <ItemID>

,where filter operator is used to reference List Item by its ID or

/_vti_bin/ListData.svc/RSSData(<ItemID>)

Note: Query options like filter could not be used in Create/Update/Delete operation

Therefore, the line:

var uri = string.Format("http://localhost/_vti_bin/ListData.svc/RSSData?$filterId eq ({0})", itemId);

should be replaced with:

var uri = string.Format("http://localhost/_vti_bin/ListData.svc/RSSData({0})", itemId);

2) Metadata type

The proper type name for List Item is Microsoft.SharePoint.DataService.RSSDataItem

Therefore, the line:

var data = "{'__metadata': { 'type': 'SP.Data.RSSDataItem' }, 'Title': '" + title + "'}";

should be replaced with:

var data = "{'__metadata': { 'type': 'Microsoft.SharePoint.DataService.RSSDataItem' }, 'Title': '" + title + "'}";

About ETag

* eTag value could be used to match any eTag value resulting in the operation being performed regardless of the actual value

Request Digests

SharePoint 2010 REST does not require the user to include a request digest value with each create, update and delete operation

share|improve this answer
1  
Awesome, I was pretty close then. I made these 2 small changes and this produces the results I expected. –  PirateEric Apr 17 at 21:05
    
Glad this helped you! :) –  Vadim Gremyachev Apr 17 at 21:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.