{"id":94655,"date":"2016-11-07T07:00:00","date_gmt":"2016-11-07T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94655"},"modified":"2019-03-13T10:33:25","modified_gmt":"2019-03-13T17:33:25","slug":"20161107-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161107-00\/?p=94655","title":{"rendered":"How do I programmatically add a folder to my Documents library?"},"content":{"rendered":"<p>Today&#8217;s Little Program adds a folder to the Documents library. Remember that Little Programs do little to no error checking. <\/p>\n<p>Today&#8217;s smart pointer library is&hellip; (rolls dice)&hellip; nothing! We&#8217;re going with raw pointers. <\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;ole2.h&gt;\n#include &lt;shlobj.h&gt;\n\nint __cdecl wmain(int argc, wchar_t** argv)\n{\n  CoInitialize(nullptr);\n  IShellLibrary* library;\n  SHLoadLibraryFromKnownFolder(FOLDERID_DocumentsLibrary,\n                               STGM_READWRITE, IID_PPV_ARGS(&amp;library));\n  for (int i = 1; i &lt; argc; i++) {\n    SHAddFolderPathToLibrary(library, argv[i]);\n  }\n  library-&gt;Release();\n  CoUninitialize();\n  return 0;\n}\n<\/pre>\n<p>This program uses some helper functions for manipulating libraries. The <code>SH&shy;Load&shy;Library&shy;From&shy;Known&shy;Folder<\/code> function is a shorthand for <code>Co&shy;Create&shy;Instance(CLSID_Shell&shy;Library)<\/code> followed by <code>IShell&shy;Library::Load&shy;Library&shy;From&shy;Known&shy;Folder<\/code>, and the <code>SH&shy;Add&shy;Folder&shy;Path&shy;To&shy;Library<\/code> function is a shorthand for <code>SH&shy;Create&shy;Item&shy;From&shy;Parsing&shy;Name<\/code> followed by <code>IShell&shy;Library::Add&shy;Folder<\/code>. <\/p>\n<p>Run this program with the full path (or paths) to the folders you want to add to the Documents Library, and&hellip; nothing happens. <\/p>\n<p>Ah, because there&#8217;s a gotcha with libraries: After you make a change to a library, you need to commit your changes. So let&#8217;s fix that: <\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;ole2.h&gt;\n#include &lt;shlobj.h&gt;\n\nint __cdecl wmain(int argc, wchar_t** argv)\n{\n  CoInitialize(nullptr);\n  IShellLibrary* library;\n  SHLoadLibraryFromKnownFolder(FOLDERID_DocumentsLibrary,\n                               STGM_READWRITE, IID_PPV_ARGS(&amp;library));\n  for (int i = 1; i &lt; argc; i++) {\n    SHAddFolderPathToLibrary(library, argv[i]);\n  }\n  <font COLOR=\"blue\">library-&gt;Commit(); \/\/ add this line<\/font>\n  library-&gt;Release();\n  CoUninitialize();\n  return 0;\n}\n<\/pre>\n<p>Okay, let&#8217;s try it again. Run this program with the full path (or paths) to the folders you want to add to the Documents Library, and hooray! the folders are added to the Documents Library. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use SHAddFolderPathToLibrary, but there&#8217;s a catch.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-94655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can use SHAddFolderPathToLibrary, but there&#8217;s a catch.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=94655"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94655\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=94655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}