<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Neta Shalom's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/netsh/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/netsh/feed.xml</link>
    <description>SharePoint outside of the box</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:41:36 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Copy OneNote Notebook to a SharePoint Location</title>
      <link>https://docs.microsoft.com/archive/blogs/netsh/copy-onenote-notebook-to-a-sharepoint-location</link>
      <pubDate>Fri, 30 Sep 2016 00:59:48 GMT</pubDate>
      <dc:creator><![CDATA[Neta Shalom]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/netsh/?p=25</guid>
      <description><![CDATA[I have recently come across a request to copy an entire OneNote Notebook programmatically from a...]]></description>
      <content:encoded><![CDATA[<span style="color: #000000;font-family: Calibri">I have recently come across a request to copy an entire OneNote Notebook programmatically from a SharePoint My Site (SP 2010) to a OneDrive location (SPO).
OneNote Notebook is not a standard Office document, as it is not a standalone file. It is associated with additional data that is stored locally by OneNote.
Therefore, copying the Notebook file alone in a straightforward fashion won't work.</span>

<span style="color: #000000;font-family: Calibri">OneNote's UI provides a way to achieve that by using the "Change Location" operation in the settings menu.</span>

<span style="color: #000000;font-family: Calibri">This operation modifies internal pointers from the current location to the desired destination.</span>

<img width="661" height="305" class="alignnone wp-image-75 size-full" alt="OneNoteLocation" src="https://msdnshared.blob.core.windows.net/media/2016/09/OneNoteLocation.jpg" />

<span style="color: #000000;font-family: Calibri">Conveniently,<a href="https://msdn.microsoft.com/en-us/library/office/jj680120.aspx">OneNote also offers an API</a> to do it programmatically in C#.</span><span style="color: #000000;font-family: Calibri">
It should be noted that an SPO CSOM (client object model) requires .NET 4 to support certain methods (for example, "SharePointOnlineCredentials").
However, OneNote API does not work with .NET 4.
Consequently, I thought of two ways to get around this and make OneNote API and CSOM .NET 4 work together:</span>
<ol>
 	<li><span style="color: #000000;font-family: Calibri">Build two separate DLLs, one for .NET 4 and one for .NET 3.5.</span></li>
 	<li><span style="color: #000000;font-family: Calibri">Have a single .NET 4 project, and set the "Embed Interop Types" reference property to "false", as shown below:</span></li>
</ol>
<span style="color: #000000;font-family: Calibri"><img width="400" height="360" class="alignnone wp-image-35 size-full" alt="onenote2" src="https://msdnshared.blob.core.windows.net/media/2016/09/onenote2.png" /></span>
<span style="color: #000000;font-family: Calibri">
Personally, I prefer the latter, because it looks more elegant and keeps the code neater :)</span>
<span style="color: #000000;font-family: Calibri">
To actually copy the Notebook itself I needed to manipulate both the file system and the OneDrive web, since <a href="https://support.microsoft.com/en-us/kb/3125202">OneDrive does not synchronize OneNote Notebooks</a>:</span>
<ol>
 	<li><span style="color: #000000;font-family: Calibri">It was required to access both the OneDrive site and the SP 2010, so I created two different client context objects for both of the sites (each with its own permissions):</span></li>
</ol>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri"><img width="485" height="62" class="alignleft wp-image-55 size-full" alt="onenote3" src="https://msdnshared.blob.core.windows.net/media/2016/09/onenote3.png" /></span></p>
<p style="text-align: justify;padding-left: 30px"><span style="color: #000000;font-family: Calibri"> </span></p>
<p style="padding-left: 30px"><img width="497" height="61" class="alignnone wp-image-65 size-full" alt="onenote4" src="https://msdnshared.blob.core.windows.net/media/2016/09/onenote4.png" /></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">2. Created a folder for the Notebook in the file system.</span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">3. Called OpenHierarchy and SyncHierarchy for the new Notebook, which is the new folder:</span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri"><img width="1116" height="457" class="alignnone size-full wp-image-96" alt="Code1-2" src="https://msdnshared.blob.core.windows.net/media/2016/09/Code1-2.jpg" /></span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">4. Changed the location <em><u>for each individual section</u></em> (only the files with the ".one" extension) in a OneNote Notebook as follows:</span></p>

<ul>
 	<li><span style="color: #000000;font-family: Calibri"><strong>Create</strong> an application object:</span></li>
</ul>
<p style="padding-left: 60px"><span style="color: #000000;font-family: Calibri">Microsoft.Office.Interop.OneNote.<span style="color: #33cccc">Application</span> onApplication = <span style="color: #0000ff">new</span> Microsoft.Office.Interop.OneNote.<span style="color: #33cccc">Application</span>();</span></p>

<ul>
 	<li><b><span style="color: #000000;font-family: Calibri"><strong>Open </strong>the hierarchy of the source section:</span></b></li>
</ul>
<p style="padding-left: 60px"><span style="color: #000000;font-family: Calibri">onApplication.OpenHierarchy(absoluteUrl, <span style="color: #0000ff">string</span>.Empty, <span style="color: #0000ff">out</span> strId);</span></p>
<p style="padding-left: 60px"><span style="color: #000000;font-family: Calibri">At this point, strId should contain a value for the section.</span></p>

<ul>
 	<li><span style="color: #000000;font-family: Calibri"><strong>Publish the section to the destination full location in the file syst</strong><strong>em:</strong></span></li>
</ul>
<p style="padding-left: 60px"><span style="color: #000000;font-family: Calibri">onApplication.Publish(strId, dest);</span></p>

<ul>
 	<li><span style="color: #000000;font-family: Calibri"><strong>Synchronize</strong> the hierarchy for this section:</span></li>
</ul>
<p style="padding-left: 60px"><span style="color: #000000;font-family: Calibri">onApplication.SyncHierarchy(strId);</span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">The "publish" method didn't work consistently with an "https" location, so I used a OneDrive local synchronized folder to pass as a destination instead of a URL.
</span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">Note that the files with the extension "onetoc2" are "Open Notebook" files. We don't need to copy them.</span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">Also note that this solution does not handle the "OneNote_RecycleBin" folder.</span></p>
<p style="padding-left: 30px"><span style="color: #000000;font-family: Calibri">5. Finally in OneDrive web, set the Content Type of the Notebook folder, because otherwise it would appear as a regular folder.</span></p>
<p style="padding-left: 30px"><span><span style="color: #000000"><span style="color: #000000;font-family: Calibri"><img width="910" height="460" class="alignnone size-full wp-image-95" alt="Code2" src="https://msdnshared.blob.core.windows.net/media/2016/09/Code21.jpg" /></span></span></span></p>
<span style="color: #000000;font-family: Calibri">That's it!</span>]]></content:encoded>
    </item>
  </channel>
</rss>