Saturday, January 09, 2010

How to update content through Google Sites API?

Reactions: 
The code to insert document data works for Google sites but for update it fails.
I tried below two calls
1
entry.setMediaSource(ms);

gives following
//IllegalArgumentException: Cannot set media source on entry with existing non-MediaContent
2
service.updateMedia(new URL(getContentFeedUrl()), newAttachment);
give
//com.google.gdata.util.InvalidEntryException,Invalid request URI
service is sites service object, entry is attachment entry

Finally the following code seems to work
     MediaStreamSource ms = new MediaStreamSource(is, fileMimeType);
     ContentQuery query = new ContentQuery(new URL(getContentFeedUrl()));
     query.setFullTextQuery(filename);
     ContentFeed contentFeed = service.getFeed(query, ContentFeed.class);
     String strAttach;
     for (AttachmentEntry entry : contentFeed.getEntries(AttachmentEntry.class)){
         strAttach = entry.getTitle().getPlainText();
         log.info("attachment "+strAttach);
         if (filename.equals(strAttach)){
              String url = ((OutOfLineContent) entry.getContent()).getUri();
              MediaContent mc = new MediaContent();
              mc.setUri(url);
              mc.setMediaSource(ms);
              mc.setMimeType(new ContentType(fileMimeType));
              entry.setContent(mc);
             return entry.updateMedia(true);//update only content and not metadata
         }
     }
Post a Comment

Disqus for dsteps blog