Update a document's content in .NET



class Demo {
  private DocumentsService service = null;

  public Demo() {
    service = new DocumentsService("google-DocUpdateTip-v1");
    service.setUserCredentials("username@gmail.com", "pa$$word");
    GDataGAuthRequestFactory reqFactory = (GDataGAuthRequestFactory)service.RequestFactory;
    reqFactory.ProtocolMajor = 3;
  }

  static void Main(string[] args) {
    Demo demo = new Demo();

    // Fetch only documents
    DocumentsListQuery query = new DocumentsListQuery("http://docs.google.com/feeds/default/private/full/-/document");
    DocumentsFeed doclistFeed = demo.service.Query(query);

    // Update first document found
    DocumentEntry entry = (DocumentEntry)doclistFeed.Entries[0];
    Console.WriteLine("Updating " + entry.Title.Text + "...");

    DocumentEntry updatedEntry = demo.UpdateDocContents(entry, "C:/Documents and Settings/replacement.doc");
    Console.WriteLine(entry.Title.Text + " Updated!, view at " + entry.AlternateUri.ToString());
  }

  public DocumentEntry UpdateDocContents(DocumentEntry entryToUpdate, String replacementFileName) {
    FileInfo fileInfo = new FileInfo(replacementFileName);
    FileStream stream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

    DocumentEntry entry = null;

    try {
      // Convert the extension to caps and strip the "." off the front
      String ext = fileInfo.Extension.ToUpper().Substring(1);

      String contentType = (String)DocumentsService.GDocumentsAllowedTypes[ext];
      if (contentType == null) {
        throw new ArgumentException("File extension '" + ext + "' is not recognized as valid.");
      }
      
      // Set ETag because we're making an update
      GDataRequestFactory factory = (GDataRequestFactory)service.RequestFactory;
      factory.CustomHeaders.Add("If-Match: " + entryToUpdate.Etag);

      Uri mediaUri = new Uri(entryToUpdate.MediaUri.ToString());
      entry = service.Update(mediaUri, stream, contentType, entryToUpdate.Title.Text) as DocumentEntry;
    } finally {
      stream.Close();
    }

    return entry;
  }
}


13 comments:

GDrive said...

small update:
instead of DocumentsService.GDocumentsAllowedTypes you need to put: DocumentsService.DocumentTypes[ext].

Besides that, the code runs without errors. However, if I look at the document in google docs, the old version appears. Any ideeas?

Shouldn't this code update the online document's content with the content from a local file?

Wonderful Jewelry said...

fashion jewelry wholesale
wholesale fashion jewelry
china jewelry wholesale
china fashion jewelry
jewelry wholesale china
fashion jewelry china
wholesale jewelry
jewelry wholesale

jhon apps said...

Mr Gdrive:
is that not giving good information ?

make facebook application

dongsheng said...

il la rponse est oui. La maison de couture fran?aise a confirm le modle Brit parce que le visage de cette collection de sacs Chanel Boy.

[Karl Lagerfeld] a le bguin au cours de cette unique, gal charismatique, qui est la fois un modle et un musicien, dans une sance photo tout l'heure, dit Chanel WWD Sac Hermès Evelyn. Cette alliance prcdemment imprvu est finalement devenu vident. Alice Dellal reprsente une incarnation idale de l', qui est unique autour de la collection de sacs Chanel Boy, qui s'efforce [d'tre] notions certainement pas conformistes de la fminit.


Yep, Dellal n'est pas vraiment conventionnelle. Ainsi que sa marque de fabrique de punk-rock uniforme, la petite de 24 ans est un batteur dans le mtal Grive bande et se rvlera tre trouv chahuter dans les rues base Londres avec Pixie Geldof. Shes loin de la blonde bombe Blake Lively, whos le visage en vigueur de Chanel Mademoiselle gamme, mais Lagerfeld est reconnu pour son go?t clectique, pralablement coule Lily Allen pour votre gamme de sac Cocoon en '09. En outre, l'androgynie Alice travaillera avec brio avec le 'Boy' collection. Comment rafra?chissante Sac Hermès Picotin.

Microsoft Office said...

Outlook 2010 provides you with a world-class experience to maintain your efficiency and keep in touch with personal and business networks. You can use download Outlook 2010, through a rich set of features and custom communication with one or more recipients.

kosovohp said...

Girl Xinh | Hot Girl | Cute Girl | Sexy Girl | Hot Girl Asian
Hot Girl | Lexi Belle | Sexy Girl | Cute Girl | Hot Asian Girl | Leah Dizon | Hot Girl Asian | Girl Xinh
socks proxy | socks5 | proxy free |proxy list

uknowme said...

il la rponse est oui. La maison de couture fran?aise a confirm le modle Brit parce que le visage de cette collection de sacs Chanel Boy. lohnrechner

top franchise opportunity

Cyntia Clara said...

Couldn't be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! Info Aksesoris HP klik disini

Happy Group said...

chuyen la vay ne admin.


Xem Nè



Xem Tử vi hàng ngày



Tin tức tổng hợp



Xem phim cực hay



12 cung sao



Soi cầu lôto xổ số miền Bắc hằng ngày



Ảnh girl xinh, girl đẹp dễ thương, girl sexy, girl bikini



Giá Bắp Ngô



Giá Ca cao



Giá Cao su



Giá Hồ tiêu



Giá Cà phê



Giá Vàng JSC các loại


Tỷ giá ngoại tệ


Buồna quá đi

Happy Group said...

Diễn Đàn Rao Vặt
Rao Vặt Cấp Platinum
Rao Vặt Cấp Vàng Bạc Đá Quý
Rao vặt Linh Tinh
› Diễn đàn
Địa chỉ hay Địa điểm văn hóa
Địa chỉ hay Ảnh viện, áo cưới
Địa chỉ hay Khu vui chơi, giải trí
Địa chỉ hay Nhà hàng,khách sạn
Địa điểm khác
Bất Động Sản Nhà mặt phố
Bất Động Sản Đất vườn
Bất Động Sản Biệt thự, liền kề
Bất Động Sản Đất nền dự án

wdmtest said...

VB.Net
Introduction
Why VB .NET?
What Is VB .NET?
What Can You Do with VB .NET?
Variables and Data Types
Variables
Declaring Variables and Constants
Data Types
Arrays

Mark said...

instaeasy review
have Your Instagram liking, comment and engage automaticlly
instaeasy review
instaeasy review
engaging with audience 24 hours a day instaeasy review
instaeasy review
InstaEasyinstaeasy review
instaeasy review
Luke Maguire
insta easy review
instagram auto engagement tool
instaeasy review
the worlds leading instagram tool
instaeasy review
Make money made easy
instaeasy bonus
Thanks for reading

Sáng Nguyễn Tiến said...

I really thank the author of this article, the article has great significance for me, it just leads me to do better things for this life. Very meaningful, thank you very much, wish you happy
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus
Vid Reaper Bonus
Mobimatic App Builder
Mobimatic Bonus
Vid Reaper Bonus
Mobimatic App Builder
Mobimatic Bonus
Vid Reaper Bonus
Mobimatic App Builder
Mobimatic Bonus
Vid Reaper Bonus
Mobimatic App Builder
Mobimatic Bonus