Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Download your DocList arbitrary file uploads in .NET


The .NET client's Download() method currently handles exporting documents, presentations, and spreadsheets. However, it cannot download PDFs or arbitrary file types. Those types cannot be exported to different formats. Instead, you need to override the Download() method in docsrequest.cs to hit the content src link:

public Stream Download(Document document) {
  string queryUri = "";
  Service s = this.Service;

  string downloadUrl = document.DocumentEntry.Content.Src.ToString();
  Uri target = new Uri(downloadUrl);

  return s.Query(target);
}

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;
  }
}


Deleting contact photos using .NET


If you want to delete a contact photo from the Contacts API using the .NET client library, you can just call ContactsService.Delete(photoURI), like so:

ContactsService service = new ContactsService("exampleCo-exampleApp-1");
service.setUserCredentials("user@example.com", "secretPassword");
ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));

ContactsFeed feed = service.Query(query);
foreach (ContactEntry entry in feed.Entries)
{
  service.Delete(entry.PhotoEditUri);
}

Be careful, the above code will delete every contact photo in your contact list. Run this against a test account only.

Create a public album for Picasa Web Albums


This is how to do it in .NET:
AlbumEntry newEntry = new AlbumEntry();
newEntry.Title.Text = "My New album";
newEntry.Summary.Text = "This is an album";
AlbumAccessor ac = new AlbumAccessor(newEntry);
ac.Access = "public";
This is how to do it in PHP:
$entry = new Zend_Gdata_Photos_AlbumEntry();
$entry->setTitle($gp->newTitle('title'));
$entry->setGphotoAccess($gp->newAccess("public"));
$createdEntry = $gp->insertAlbumEntry($entry);

Setting publishing_priority attribute in .NET


For items to be published immediately using the Google Base API, you must set the publishing_priority to 'high'. The default is 'low'.

If you're using 1.2.3+ of the .NET client:
GBaseEntry entry = new GBaseEntry();
entry.PublishingPriority = new PublishingPriority("high");
GBaseEntry myEntry = service.Insert(GBaseUriFactory.Default.ItemsFeedUri, entry); 
Older versions:
GBaseEntry entry = new GBaseEntry();

XmlDocument xmlDocument = new XmlDocument();
XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, 
  "app", "control", "http://purl.org/atom/app#");
XmlNode pp = xmlDocument.CreateNode(XmlNodeType.Element, 
  "gm", "publishing_priority", "http://base.google.com/ns-metadata/1.0");

pp.InnerText = "high";
node.AppendChild(pp);

entry.ExtensionElements.Add(node);
GBaseEntry myEntry = service.Insert(GBaseUriFactory.Default.ItemsFeedUri, entry);

Getting Blogger Post URLs Using C#


Want to look up the URL for a post retrieved using the Blogger API? If you're using the .NET client library, you can do it with one line of C# code:

string postUrl = entry.AlternateUri.Content;

Save A Google Docs Document As A File In .NET


Given a DocumentEntry, save to local file "C:\example.html"
Uri documentUri = new Uri(entry.Content.AbsoluteUri);
Stream stream = service.Query(documentUri);
StreamReader streamReader = new StreamReader(stream);
StreamWriter streamWriter = new StreamWriter("C:\\example.html");
string line = "";
while( (line = streamReader.ReadLine()) != null) {
    streamWriter.WriteLine(line);
}
streamReader.Close();
streamWriter.Close();