<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Custom SharePoint Archives | DMC, Inc.</title>
	<atom:link href="https://static.dmcinfo.com/blog/tag/custom-sharepoint/feed/index.xml" rel="self" type="application/rss+xml" />
	<link></link>
	<description></description>
	<lastBuildDate>Wed, 17 Jun 2026 20:35:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>

<image>
	<url>https://static.dmcinfo.com/wp-content/uploads/2025/04/site-icon-150x150.png</url>
	<title>Custom SharePoint Archives | DMC, Inc.</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Using JSLink with SharePoint Lists to Render Custom WebParts</title>
		<link>https://static.dmcinfo.com/blog/21454/using-jslink-with-sharepoint-lists-to-render-custom-webparts/</link>
		
		<dc:creator><![CDATA[DMC]]></dc:creator>
		<pubDate>Wed, 05 Jun 2019 08:46:33 +0000</pubDate>
				<category><![CDATA[Digital Workplace Solutions]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Custom SharePoint]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[JSLink]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/21454/using-jslink-with-sharepoint-lists-to-render-custom-webparts/</guid>

					<description><![CDATA[<p>Say you have a basic list in SharePoint and want to display the list data on your SharePoint page. A typical list that looks like a grid can be pretty dull, so it is common that a company or developer would want to show the list in a way that is not explicitly made available [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/21454/using-jslink-with-sharepoint-lists-to-render-custom-webparts/">Using JSLink with SharePoint Lists to Render Custom WebParts</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Say you have a basic list in <a href="/services/digital-workplace-solutions/microsoft-consulting-services/sharepoint">SharePoint</a> and want to display the list data on your SharePoint page. A typical list that looks like a grid can be pretty dull, so it is common that a company or developer would want to show the list in a way that is not explicitly made available in SharePoint.</p>



<p class="wp-block-paragraph">Sure, SharePoint lets you filter out items/columns, etc., but&nbsp;it may not look nice as a simple grid, and you want to add some extra functionality. Lists and libraries are great at storing data in SharePoint, but when you add a simple grid view of the data on a page, it leaves you striving for something <a href="/services/digital-workplace-solutions/microsoft-consulting-services/sharepoint/sharepoint-look-and-feel-customization">more visually pleasing</a>.</p>



<p class="wp-block-paragraph">Believe it or not, with a little coding knowledge,&nbsp;you can write code to override what a list/library WebPart looks like on your SharePoint page! For classic SharePoint pages, we can easily make use of a technology that SharePoint has built in to display virtually whatever you&#8217;d like with the data from that WebPart.</p>



<p class="wp-block-paragraph">This blog will describe how to use JavaScript on your SharePoint page and how to use SharePoint&#8217;s built-in JSLink technology. It takes a relatively small amount of code for a pretty cool outcome.</p>



<h2 class="wp-block-heading" id="h-use-case">Use Case</h2>



<p class="wp-block-paragraph">Say we have a SharePoint list called <strong>&#8220;Employee Info&#8221;</strong> that has three columns: <strong>Name, Department, and Employee Picture</strong>. Rather than the user having to look at the out of the box grid WebPart for the view, we want to show something easier&nbsp;to read and better looking.</p>



<p class="wp-block-paragraph">For each person, I had an idea&nbsp;to show a&nbsp;bordered box that includes&nbsp;the employee name, picture, and department. We have all of the data that we need, and we need to make what is inside this WebPart look a bit more presentable.</p>



<p class="wp-block-paragraph">To do this, we are going to write&nbsp;<strong>JavaScript code</strong> that will automatically be run on the page load when the WebPart is rendering. Before SharePoint renders the view of the list on the page, we will step in and instead give SharePoint something else to render.</p>



<p class="wp-block-paragraph">As a developer, maybe some of the wordage just used is already getting your wheels turning. You can probably imagine some of the steps we are going to have to take to make this work. On a high level, with our code, we will need to make sure to do the following:</p>



<ol class="wp-block-list">
<li>Hook up to/find the SharePoint list that we want to manipulate
 
 
<ol class="wp-block-list" style="margin-bottom:40px; list-style-type:lower-alpha">
  

<li style="margin-bottom:20px;"><strong>Sharepoint&apos;s JSlink </strong>feature on a list WebPart&nbsp;gives us direct access to the list and the items in it. So your JavaScript code will get passed the elements in the list.</li>


  

<li style="margin-bottom:20px;">If you want to configure the list WebPart to be a specific view (say, only showing Employees where their &quot;Department&quot; column is equal to &quot;Accounting&quot;), there is no more work you need to do, because the sorting is done by SharePoint in the configuration and doesn&apos;t have to be done in the code.</li>


 </ol>
</li>



<li>For each of the items in the list, we need to give SharePoint the item that we want to show.
 
 
<ol class="wp-block-list" style="margin-bottom:40px; list-style-type:lower-alpha">
  

<li style="margin-bottom:20px;">In this case, we&apos;re getting all the elements from the list but just displaying them in a visually pleasing&nbsp;way.
  

<ol class="wp-block-list" style="margin-bottom:40px; list-style-type:lower-roman">
   

<li style="margin-bottom:20px;">In HTML terms, for each employee, we will probably want to render a <strong>div, </strong>and inside the div, there could be an <strong>img tag</strong> for the picture and <strong>divs</strong> for the name and department. We can use CSS to add our custom styles.</li>


  </ol>


  </li>


 </ol>
</li>
</ol>



<p class="wp-block-paragraph">We will plan to create a JavaScript file that will interact with the page and overwrite the view of our WebPart.&nbsp;Once we point our WebPart&nbsp;to the code we have written, we&#8217;ll be good to go!</p>



<p class="wp-block-paragraph">Now, because we are overriding what is rendered, we are getting rid of the HTML and everything that was there in the first place related to the look of the normal SharePoint WebPart. As a result, we&#8217;ll have to return our custom content to SharePoint. Below are some of the technologies/languages we&#8217;ll use.</p>



<h2 class="wp-block-heading" id="h-our-toolbox">Our Toolbox</h2>



<ul class="wp-block-list">
<li>SharePoint List
 
 
<ul class="wp-block-list">
<li>This is where our data resides. In our example, we created an &#8220;Employees&#8221; list in the <strong>Site Contents</strong> of our SharePoint page</li>
</ul>
</li>



<li>List WebPart
 
 
<ul class="wp-block-list">
<li>On our classic SharePoint page, we added a WebPart&nbsp;to display the employee list.</li>
</ul>
</li>



<li>JavaScript File
 
 
<ul class="wp-block-list">
<li>This file contains all of our logic. We upload this file to our site&#8217;s <strong>Site Assets</strong> folder, copy the URL of it, and add this URL to the JSLink section of our list <strong>WebPart&#8217;s settings pane</strong>. Under the <strong>miscellaneous section</strong> of the settings pane, you will find the JSLink option.</li>
</ul>
</li>



<li>HTML
 
 
<ul class="wp-block-list">
<li>We return HTML markup from our JavaScript, which is what the WebPart renders instead of its default HTML.</li>
</ul>
</li>



<li>CSS File
 
 
<ul class="wp-block-list">
<li>For when the HTML that we return from the JavaScript is on the page, we can have CSS styles uploaded to SharePoint that will style the new content of the WebPart to our liking.</li>



<li>We&#8217;ll put this CSS File&nbsp;into&nbsp;<strong>Site Assets</strong> as well.</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph">Alright, enough of the setup, here is what the full JavaScript file for this example looks like. Below, I&#8217;ll go into detail about certain parts of this JavaScript file.</p>



<p class="wp-block-paragraph">The function where we&#8217;re&nbsp;doing almost all of the work is in <strong>RenderViewBodyTemplate</strong>. I&#8217;ll explain what&#8217;s going on here and then go over some of the other important&nbsp;parts of what makes this work:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">JavaScript</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly> /* this is the function that SharePoint normally calls that we are overriding.
    By overriding this function, when SharePoint tries to render our custom webpart,
    it will display whatever we return from this function */
    DMCExample.EmployeeListScript.RenderViewBodyTemplate = function(renderCtx) {
        // renderCtx is the render context, which SharePoint passes to this function

        // the ListData property will have all of the data for this webpart view
        // ListData can be iterated through to get information from each list item
        var listData = renderCtx.ListData;

        // the number of items in the list
        var rowCount = listData.Row.length;
        if (renderCtx.Templates.Body === '') {
            return RenderViewTemplate(renderCtx);
        }

        // the opening div tag for our webpart. We will remember to close it after we have added all of our elements
        // also adding a custom id for this div so we can easily style it with our css
        var EmployeesWebpartHTML = '&lt;div id="EmployeesWebpartDiv">';
        for (var i = 0; i &lt; rowCount; i++) {
            EmployeesWebpartHTML +=
                '&lt;div class="employee-container">' +
                    // this is the internal name of the field on our example list.
                    '&lt;img src="'+listData.Row&#91;i&#93;.EmployeePictureLink+'" />' +
                    '&lt;div>' +
                        '&lt;span class="employee-name">'+listData.Row&#91;i&#93;.EmployeeName+'&lt;/span>' +
                    '&lt;/div>' +
                '&lt;/div>';
        }
        
        // our closing tag that we needed to remember
        EmployeesWebpartHTML += '&lt;/div>';

        return EmployeesWebpartHTML;
    };</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4"> </span><span style="color: #6A9955">/* this is the function that SharePoint normally calls that we are overriding.</span></span>
<span class="line"><span style="color: #6A9955">    By overriding this function, when SharePoint tries to render our custom webpart,</span></span>
<span class="line"><span style="color: #6A9955">    it will display whatever we return from this function */</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">DMCExample</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">EmployeeListScript</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">RenderViewBodyTemplate</span><span style="color: #D4D4D4"> = </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">renderCtx</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// renderCtx is the render context, which SharePoint passes to this function</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// the ListData property will have all of the data for this webpart view</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// ListData can be iterated through to get information from each list item</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">listData</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">renderCtx</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">ListData</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// the number of items in the list</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">rowCount</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">listData</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Row</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">length</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">renderCtx</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Templates</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Body</span><span style="color: #D4D4D4"> === </span><span style="color: #CE9178">&apos;&apos;</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">RenderViewTemplate</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">renderCtx</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// the opening div tag for our webpart. We will remember to close it after we have added all of our elements</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// also adding a custom id for this div so we can easily style it with our css</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">EmployeesWebpartHTML</span><span style="color: #D4D4D4"> = </span><span style="color: #CE9178">&apos;&lt;div id=&quot;EmployeesWebpartDiv&quot;&gt;&apos;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> (</span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> = </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">; </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> &lt; </span><span style="color: #9CDCFE">rowCount</span><span style="color: #D4D4D4">; </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4">++) {</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #9CDCFE">EmployeesWebpartHTML</span><span style="color: #D4D4D4"> +=</span></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #CE9178">&apos;&lt;div class=&quot;employee-container&quot;&gt;&apos;</span><span style="color: #D4D4D4"> +</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #6A9955">// this is the internal name of the field on our example list.</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #CE9178">&apos;&lt;img src=&quot;&apos;</span><span style="color: #D4D4D4">+</span><span style="color: #9CDCFE">listData</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Row</span><span style="color: #D4D4D4">&#91;</span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4">&#93;.</span><span style="color: #9CDCFE">EmployeePictureLink</span><span style="color: #D4D4D4">+</span><span style="color: #CE9178">&apos;&quot; /&gt;&apos;</span><span style="color: #D4D4D4"> +</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #CE9178">&apos;&lt;div&gt;&apos;</span><span style="color: #D4D4D4"> +</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #CE9178">&apos;&lt;span class=&quot;employee-name&quot;&gt;&apos;</span><span style="color: #D4D4D4">+</span><span style="color: #9CDCFE">listData</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Row</span><span style="color: #D4D4D4">&#91;</span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4">&#93;.</span><span style="color: #9CDCFE">EmployeeName</span><span style="color: #D4D4D4">+</span><span style="color: #CE9178">&apos;&lt;/span&gt;&apos;</span><span style="color: #D4D4D4"> +</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #CE9178">&apos;&lt;/div&gt;&apos;</span><span style="color: #D4D4D4"> +</span></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #CE9178">&apos;&lt;/div&gt;&apos;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">        }</span></span>
<span class="line"><span style="color: #D4D4D4">        </span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// our closing tag that we needed to remember</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">EmployeesWebpartHTML</span><span style="color: #D4D4D4"> += </span><span style="color: #CE9178">&apos;&lt;/div&gt;&apos;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">EmployeesWebpartHTML</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">    };</span></span></code></pre></div>



<p class="wp-block-paragraph">As you can see, we are programmatically constructing the HTML, and then returning it, which sends it to SharePoint when it is trying to render the WebPart.</p>



<p class="wp-block-paragraph">Because we added some classes, we can now easily style these elements with CSS. In the<strong> for-loop</strong>, we are iterating over the items in the list and inserting them into our HTML. &nbsp;</p>



<p class="wp-block-paragraph">The name of the fields is&nbsp;<strong>&#8220;EmployeePictureLink&#8221;</strong> and <strong>&#8220;EmployeeName&#8221;</strong>.</p>



<h2 class="wp-block-heading" style="margin-bottom:20px;">SharePoint&apos;s Internal Field Names</h2>



<p class="wp-block-paragraph"><strong>Finding Internal Name of List Column</strong></p>



<p class="wp-block-paragraph">When referencing a column from your SharePoint list in your JavaScript, you&#8217;re going to want to use the internal name of the column. These internal names are what SharePoint stores the name of the column as in the backend.&nbsp;These internal names are what you&#8217;ll reference the field as when getting the information.</p>



<p class="wp-block-paragraph">I&#8217;ll tell you a straightforward way to get these internal names so you can use them, but here are some <strong>common tips/rules</strong> for internal names:</p>



<ul class="wp-block-list">
<li>If your column name is one word, like &#8220;Department,&#8221; SharePoint will&nbsp;store the internal name as &#8220;Department.&#8221;</li>



<li>SharePoint changes the name when you&nbsp;have a space in your column name. For example, &#8220;Contact Name&#8221; will probably be &#8220;Contact_x0020_Name&#8221; because the text in the middle is&nbsp;how SharePoint stores spaces.</li>



<li>To find the internal name of a column, go to your list in SharePoint and then go to <strong>List Settings</strong>. You can find the List settings by <strong>clicking the gear</strong> in the top right of the page. Once you&#8217;re in list settings, scroll to the bottom and click on the name of the column you want the internal name for. In the new page that you are taken to, the field name should be at the end of the URL of that page.</li>
</ul>



<figure class="wp-block-image"><img decoding="async" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/SharePoint-URL-Contact-Name.png" alt="SharePoint URL for contact name"/></figure>



<p class="wp-block-paragraph">While this is all you&#8217;ll need for the majority of the functionality, here are some other aspects to make sure that your code works:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">JavaScript</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>DMCExample.EmployeeListScript.Create = function() {
        var viewContext = {};

        viewContext.Templates = {};
        viewContext.Templates.View = DMCExample.HomeLinks.RenderViewBodyTemplate;
        viewContext.OnPreRender = DMCExample.HomeLinks.OnViewPreRender;
        viewContext.OnPostRender = DMCExample.HomeLinks.OnViewPostRender;
        viewContext.BaseViewID = 100; //Note: This is the BaseViewID we'll set when the renderlistview is called.

        // Now override the RenderListView once the ClientTemplates.JS has been called
        ExecuteOrDelayUntilScriptLoaded(function() {
            //Take a copy of the existing Microsoft Definition of RenderListView
            var oldRenderListView = RenderListView;

            //Now redefine RenderListView with our override
            RenderListView = function(ctx, webPartID) {
                //Check the context of the currently rendering List view
                // This long string of numbers and letters is the GUID of the list
                if (
                    ctx.listName &amp;&amp;
                    ctx.listName.toUpperCase() ===
                        '{BB9516A6-4E02-4048-8D66-1B2D2109D096}'.toUpperCase()
                ) {
                    var isInEditMode = false;
                    var pageForm = document.forms&#91;MSOWebPartPageFormName&#93;;
                    if (pageForm) {
                        if (pageForm.MSOLayout_InDesignMode) {
                            isInEditMode = pageForm.MSOLayout_InDesignMode.value === '1';
                        }
                        if (pageForm._wikiPageMode) {
                            isInEditMode = isInEditMode || pageForm._wikiPageMode.value === 'Edit';
                        }
                    }
                    if (!isInEditMode) {
                        //Override the BaseViewID if it's the one we want.
                        ctx.BaseViewID = 100;
                    }
                }</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">DMCExample</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">EmployeeListScript</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">Create</span><span style="color: #D4D4D4"> = </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4">() {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4"> = {};</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Templates</span><span style="color: #D4D4D4"> = {};</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Templates</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">View</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">DMCExample</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">HomeLinks</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">RenderViewBodyTemplate</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">OnPreRender</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">DMCExample</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">HomeLinks</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">OnViewPreRender</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">OnPostRender</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">DMCExample</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">HomeLinks</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">OnViewPostRender</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">BaseViewID</span><span style="color: #D4D4D4"> = </span><span style="color: #B5CEA8">100</span><span style="color: #D4D4D4">; </span><span style="color: #6A9955">//Note: This is the BaseViewID we&apos;ll set when the renderlistview is called.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #6A9955">// Now override the RenderListView once the ClientTemplates.JS has been called</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">ExecuteOrDelayUntilScriptLoaded</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">function</span><span style="color: #D4D4D4">() {</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #6A9955">//Take a copy of the existing Microsoft Definition of RenderListView</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">oldRenderListView</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">RenderListView</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #6A9955">//Now redefine RenderListView with our override</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #DCDCAA">RenderListView</span><span style="color: #D4D4D4"> = </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">ctx</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">webPartID</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #6A9955">//Check the context of the currently rendering List view</span></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #6A9955">// This long string of numbers and letters is the GUID of the list</span></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #9CDCFE">ctx</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">listName</span><span style="color: #D4D4D4"> &amp;&amp;</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #9CDCFE">ctx</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">listName</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">toUpperCase</span><span style="color: #D4D4D4">() ===</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #CE9178">&apos;{BB9516A6-4E02-4048-8D66-1B2D2109D096}&apos;</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">toUpperCase</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">                ) {</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">isInEditMode</span><span style="color: #D4D4D4"> = </span><span style="color: #569CD6">false</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">pageForm</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">document</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">forms</span><span style="color: #D4D4D4">&#91;</span><span style="color: #9CDCFE">MSOWebPartPageFormName</span><span style="color: #D4D4D4">&#93;;</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">pageForm</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">pageForm</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">MSOLayout_InDesignMode</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">                            </span><span style="color: #9CDCFE">isInEditMode</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">pageForm</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">MSOLayout_InDesignMode</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">value</span><span style="color: #D4D4D4"> === </span><span style="color: #CE9178">&apos;1&apos;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">                        }</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">pageForm</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">_wikiPageMode</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">                            </span><span style="color: #9CDCFE">isInEditMode</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">isInEditMode</span><span style="color: #D4D4D4"> || </span><span style="color: #9CDCFE">pageForm</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">_wikiPageMode</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">value</span><span style="color: #D4D4D4"> === </span><span style="color: #CE9178">&apos;Edit&apos;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">                        }</span></span>
<span class="line"><span style="color: #D4D4D4">                    }</span></span>
<span class="line"><span style="color: #D4D4D4">                    </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (!</span><span style="color: #9CDCFE">isInEditMode</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #6A9955">//Override the BaseViewID if it&apos;s the one we want.</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #9CDCFE">ctx</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">BaseViewID</span><span style="color: #D4D4D4"> = </span><span style="color: #B5CEA8">100</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">                    }</span></span>
<span class="line"><span style="color: #D4D4D4">                }</span></span></code></pre></div>



<p class="wp-block-paragraph">The BaseViewID we set to 100 is a bit arbitrary.&nbsp;100 is, more than likely, higher than the number of other WebParts not using this JSLink code on the page. <strong>BaseViewID</strong> is what links together all of the functions in this JavaScript file. The main thing to note is, if you have another JSLink file, make sure that it has a different BaseViewID. I usually start at 100 and count up.</p>



<p class="wp-block-paragraph">You may also notice the long string <strong>&#8220;{BB9516A6-4E02-4048-8D66-1B2D2109D096}&#8221;</strong>. This string is the GUID (unique identifier) of my employee&nbsp;list. JSLink, by nature, runs for every WebPart on the page, even if you only linked it into one WebPart.&nbsp;This check makes sure that we&#8217;re looking at the WebPart for our employee&nbsp;list.</p>



<p class="wp-block-paragraph">If that is the case, we will set the BaseViewID on the context object, which matches the initial BaseViewID that we set up, thus linking the functions together and allowing them to work when we call <strong>SPClientTemplates.TemplateManager.RegisterTemplateOverrides(viewContext);</strong></p>



<p class="wp-block-paragraph">Towards the bottom of the JavaScript file, the code reads:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">JavaScript</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>if (!isInEditMode) {
                        //Override the BaseViewID if it's the one we want.
                        ctx.BaseViewID = 100;
                    }
                }

                //now call the original RenderListView with the updated ctx object if applicable
                oldRenderListView(ctx, webPartID);
            };
        }, 'ClientTemplates.js');

        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(viewContext);
    };

    ExecuteOrDelayUntilScriptLoaded(DMCExample.HomeLinks.Create, 'clienttemplates.js');
})();</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (!</span><span style="color: #9CDCFE">isInEditMode</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #6A9955">//Override the BaseViewID if it&apos;s the one we want.</span></span>
<span class="line"><span style="color: #D4D4D4">                        </span><span style="color: #9CDCFE">ctx</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">BaseViewID</span><span style="color: #D4D4D4"> = </span><span style="color: #B5CEA8">100</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">                    }</span></span>
<span class="line"><span style="color: #D4D4D4">                }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #6A9955">//now call the original RenderListView with the updated ctx object if applicable</span></span>
<span class="line"><span style="color: #D4D4D4">                </span><span style="color: #DCDCAA">oldRenderListView</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">ctx</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">webPartID</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">            };</span></span>
<span class="line"><span style="color: #D4D4D4">        }, </span><span style="color: #CE9178">&apos;ClientTemplates.js&apos;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">SPClientTemplates</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">TemplateManager</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">RegisterTemplateOverrides</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">viewContext</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    };</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">ExecuteOrDelayUntilScriptLoaded</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">DMCExample</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">HomeLinks</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Create</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&apos;clienttemplates.js&apos;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">})();</span></span></code></pre></div>



<p class="wp-block-paragraph">At the end of our code, we call a function named <strong>ExecuteOrDelayUntilScriptLoaded</strong>. As you can see, the first parameter we pass into that function is the name of our main function called <strong>&#8220;Create.&#8221; </strong></p>



<p class="wp-block-paragraph">The second parameter is the name of a script. The file that we have in the second parameter is <strong>&#8220;clienttemplates.js.&#8221;</strong> This built-in SharePoint function&nbsp;means &#8220;don&#8217;t run the object in the first parameter until the second parameter is loaded.&#8221; Therefore, we don&#8217;t run our <strong>Create</strong> function until <strong>clienttemplates.js</strong> is loaded.</p>



<p class="wp-block-paragraph">This file, &#8220;clienttemplates.js,&#8221; is the core file that SharePoint loads to allow for client-side rendering. We need this file to load before our code would be able to work.</p>



<p class="wp-block-paragraph">Now that we have created our code, we are ready to put it all into SharePoint and watch it work!</p>



<ul class="wp-block-list">
<li>Make sure that your JavaScript file (and CSS file if you have one) are in your <strong>Site Assets </strong>folder for your SharePoint site.

 
 
<ul class="wp-block-list">
<li>Copy the link to your notepad and replace everything <strong>before</strong> /SiteAssets with a &#8220;~,&#8221; and then either <strong>&#8220;site&#8221;</strong> or <strong>&#8220;sitecollection&#8221;</strong>. You will put &#8220;sitecollection&#8221; if your JS file is in the SiteAssets folder of a root site of a site collection, or &#8220;site&#8221; otherwise. So the link would appear as <strong>~sitecollection/SiteAssets/…</strong></li>
</ul>
</li>



<li>On your SharePoint page, edit the page and click on the properties for the WebPart. A pane on the right-hand side of the page should pop up for the WebPart settings. Under the<strong> miscellaneous section</strong> of those settings, in the JSLink box, place that edited URL.</li>



<li>Now the JSLink should be running!</li>
</ul>



<h2 class="wp-block-heading" id="h-adding-the-style-files">Adding the Style Files</h2>



<ul class="wp-block-list">
<li>In your site settings, under the <strong>&#8220;Look and Feel&#8221;</strong> group of options, click <strong>&#8220;Master Page.&#8221;</strong> If this doesn&#8217;t show up, you&#8217;ll need to enable the <strong>SharePoint Server Publishing</strong>&nbsp;<strong>Infrastructure</strong> in Site Features.</li>



<li>If not activated, go to <strong>Site Collection features </strong>and activate <strong>SharePoint Server Publishing Infrastructure</strong>
 
 
<ul class="wp-block-list">
<li>Under there, you can go to the &#8220;Alternate CSS&#8221; area and post the link to your CSS file in it.</li>



<li>Now, the CSS that is in your Site Assets folder should be used when the page loads. For example, if in your JSLink file, one of the <strong>divs</strong> that you return to the view has a <strong>classname </strong>of <strong>&#8220;employee-picture,&#8221;</strong> in your CSS you could reference that class to add your styles!</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph">At this point, with the tools and code provided above, you should be able to move forward and render SharePoint lists on the page to your liking. The sky is the limit with JSLink, and with it, we can turn this easily stored data into beautiful and functional custom WebPart to fit your needs!</p>



<p class="wp-block-paragraph">Learn more about DMC&#8217;s <a href="/services/digital-workplace-solutions/microsoft-consulting-services/sharepoint">SharePoint Consulting Services</a> and <a href="/services/digital-workplace-solutions">Digital Workplace Solutions</a> team. <a href="/contact">Contact us</a> for your next custom SharePoint project.</p>
<p>The post <a href="https://static.dmcinfo.com/blog/21454/using-jslink-with-sharepoint-lists-to-render-custom-webparts/">Using JSLink with SharePoint Lists to Render Custom WebParts</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Upload a File to SharePoint On-Premises Using Python</title>
		<link>https://static.dmcinfo.com/blog/24542/how-to-upload-a-file-to-sharepoint-on-premises-using-python/</link>
		
		<dc:creator><![CDATA[Curtis Weir]]></dc:creator>
		<pubDate>Thu, 09 Mar 2017 12:12:06 +0000</pubDate>
				<category><![CDATA[Digital Workplace Solutions]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Custom SharePoint]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/24542/how-to-upload-a-file-to-sharepoint-on-premises-using-python/</guid>

					<description><![CDATA[<p>There are several tools out there for interacting with SharePoint through a Python script, but today, I am going to demonstrate a very simple way to upload a file to your SharePoint environment&#160;with minimal overhead. This can be useful for users running on Linux environments, such as&#160;a Raspberry PI, who want to script some functionality. [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/24542/how-to-upload-a-file-to-sharepoint-on-premises-using-python/">How to Upload a File to SharePoint On-Premises Using Python</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">There are several tools out there for interacting with SharePoint through a Python script, but today, I am going to demonstrate a very simple way to upload a file to your SharePoint environment&nbsp;with minimal overhead. This can be useful for users running on Linux environments, such as&nbsp;a Raspberry PI, who want to script some functionality.</p>



<h2 class="wp-block-heading" id="h-using-octoprint-to-monitor-3d-printing">Using Octoprint to Monitor 3D Printing</h2>



<p class="wp-block-paragraph">I recently set up a tool called <a href="http://octoprint.org/" target="_blank">Octoprint</a>&nbsp;on a Raspberry Pi, and am using it to control and monitor the 3D printer here at DMC.</p>



<p class="wp-block-paragraph">The tool generates timelapses after each print, and it started to take up a lot of space on the Pi’s storage.</p>



<p class="wp-block-paragraph"><iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/Ndu9Qaakv44" width="560"></iframe></p>



<p class="wp-block-paragraph">I used the following script to take newly rendered timelapses and upload them to SharePoint. This allows users to easily access their print timelapses&nbsp;and frees space on the pi.</p>



<h2 class="wp-block-heading" id="h-setup">Setup</h2>



<p class="wp-block-paragraph">You will need to install the requests package and NTLM authentication package on your environment. In a Linux environment, type the following commands:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Python</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>pip install requests

pip install requests_ntlm</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">pip install requests</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">pip install requests_ntlm</span></span></code></pre></div>



<h2 class="wp-block-heading" id="h-python-script">Python Script</h2>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Python</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>#Import required packages
import sys
import requests
from requests_ntlm import HttpNtlmAuth

#Read filename (relative path) from command line
fileName = sys.argv&#91;1&#93;

#Enter your SharePoint site and target library
sharePointUrl = 'https://your.sharepointsite.com/'
folderUrl = '/Your/SharePoint/DocumentLibrary'

#Sets up the url for requesting a file upload
requestUrl = sharePointUrl + '/_api/web/getfolderbyserverrelativeurl(\'' + folderUrl + '\')/Files/add(url=\'' + fileName + '\',overwrite=true)'

#Read in the file that we are going to upload
file = open(fileName, 'rb')

#Setup the required headers for communicating with SharePoint 
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose'}

#Execute a request to get the FormDigestValue. This will be used to authenticate our upload request
r = requests.post(sharePointUrl + "/_api/contextinfo",auth=HttpNtlmAuth('Domain\\username','password'), headers=headers)
formDigestValue = r.json()&#91;'d'&#93;&#91;'GetContextWebInformation'&#93;&#91;'FormDigestValue'&#93;

#Update headers to use the newly acquired FormDigestValue
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}

#Execute the request. If you run into issues, inspect the contents of uploadResult
uploadResult = requests.post(requestUrl,auth=HttpNtlmAuth('Domain\\username','password'), headers=headers, data=file.read())</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955">#Import required packages</span></span>
<span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> sys</span></span>
<span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> requests</span></span>
<span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> requests_ntlm </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> HttpNtlmAuth</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Read filename (relative path) from command line</span></span>
<span class="line"><span style="color: #D4D4D4">fileName = sys.argv&#91;</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">&#93;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Enter your SharePoint site and target library</span></span>
<span class="line"><span style="color: #D4D4D4">sharePointUrl = </span><span style="color: #CE9178">&apos;https://your.sharepointsite.com&/#39;</span></span>
<span class="line"><span style="color: #D4D4D4">folderUrl = </span><span style="color: #CE9178">&apos;/Your/SharePoint/DocumentLibrary&apos;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Sets up the url for requesting a file upload</span></span>
<span class="line"><span style="color: #D4D4D4">requestUrl = sharePointUrl + </span><span style="color: #CE9178">&apos;/_api/web/getfolderbyserverrelativeurl(</span><span style="color: #D7BA7D">\&apos;</span><span style="color: #CE9178">&apos;</span><span style="color: #D4D4D4"> + folderUrl + </span><span style="color: #CE9178">&apos;</span><span style="color: #D7BA7D">\&apos;</span><span style="color: #CE9178">)/Files/add(url=</span><span style="color: #D7BA7D">\&apos;</span><span style="color: #CE9178">&apos;</span><span style="color: #D4D4D4"> + fileName + </span><span style="color: #CE9178">&apos;</span><span style="color: #D7BA7D">\&apos;</span><span style="color: #CE9178">,overwrite=true)&apos;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Read in the file that we are going to upload</span></span>
<span class="line"><span style="color: #D4D4D4">file = </span><span style="color: #DCDCAA">open</span><span style="color: #D4D4D4">(fileName, </span><span style="color: #CE9178">&apos;rb&apos;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Setup the required headers for communicating with SharePoint </span></span>
<span class="line"><span style="color: #D4D4D4">headers = {</span><span style="color: #CE9178">&apos;Content-Type&apos;</span><span style="color: #D4D4D4">: </span><span style="color: #CE9178">&apos;application/json; odata=verbose&apos;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&apos;accept&apos;</span><span style="color: #D4D4D4">: </span><span style="color: #CE9178">&apos;application/json;odata=verbose&apos;</span><span style="color: #D4D4D4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Execute a request to get the FormDigestValue. This will be used to authenticate our upload request</span></span>
<span class="line"><span style="color: #D4D4D4">r = requests.post(sharePointUrl + </span><span style="color: #CE9178">&quot;/_api/contextinfo&quot;</span><span style="color: #D4D4D4">,</span><span style="color: #9CDCFE">auth</span><span style="color: #D4D4D4">=HttpNtlmAuth(</span><span style="color: #CE9178">&apos;Domain</span><span style="color: #D7BA7D">\\</span><span style="color: #CE9178">username&apos;</span><span style="color: #D4D4D4">,</span><span style="color: #CE9178">&apos;password&apos;</span><span style="color: #D4D4D4">), </span><span style="color: #9CDCFE">headers</span><span style="color: #D4D4D4">=headers)</span></span>
<span class="line"><span style="color: #D4D4D4">formDigestValue = r.json()&#91;</span><span style="color: #CE9178">&apos;d&apos;</span><span style="color: #D4D4D4">&#93;&#91;</span><span style="color: #CE9178">&apos;GetContextWebInformation&apos;</span><span style="color: #D4D4D4">&#93;&#91;</span><span style="color: #CE9178">&apos;FormDigestValue&apos;</span><span style="color: #D4D4D4">&#93;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Update headers to use the newly acquired FormDigestValue</span></span>
<span class="line"><span style="color: #D4D4D4">headers = {</span><span style="color: #CE9178">&apos;Content-Type&apos;</span><span style="color: #D4D4D4">: </span><span style="color: #CE9178">&apos;application/json; odata=verbose&apos;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&apos;accept&apos;</span><span style="color: #D4D4D4">: </span><span style="color: #CE9178">&apos;application/json;odata=verbose&apos;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&apos;x-requestdigest&apos;</span><span style="color: #D4D4D4"> : formDigestValue}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">#Execute the request. If you run into issues, inspect the contents of uploadResult</span></span>
<span class="line"><span style="color: #D4D4D4">uploadResult = requests.post(requestUrl,</span><span style="color: #9CDCFE">auth</span><span style="color: #D4D4D4">=HttpNtlmAuth(</span><span style="color: #CE9178">&apos;Domain</span><span style="color: #D7BA7D">\\</span><span style="color: #CE9178">username&apos;</span><span style="color: #D4D4D4">,</span><span style="color: #CE9178">&apos;password&apos;</span><span style="color: #D4D4D4">), </span><span style="color: #9CDCFE">headers</span><span style="color: #D4D4D4">=headers, </span><span style="color: #9CDCFE">data</span><span style="color: #D4D4D4">=file.read())</span></span></code></pre></div>



<p class="wp-block-paragraph">Browse to your SharePoint site, and you should now see the newly uploaded file.</p>



<p class="wp-block-paragraph">Learn more about DMC&#8217;s&nbsp;<a href="https://static.dmcinfo.com/services/digital-workplace-solutions/microsoft-consulting-services/sharepoint">SharePoint Consulting Services</a>&nbsp;and&nbsp;<a href="https://static.dmcinfo.com/services/digital-workplace-solutions">Digital Workplace Solutions</a>&nbsp;team.&nbsp;<a href="https://static.dmcinfo.com/contact">Contact us</a>&nbsp;for your next custom SharePoint project.</p>
<p>The post <a href="https://static.dmcinfo.com/blog/24542/how-to-upload-a-file-to-sharepoint-on-premises-using-python/">How to Upload a File to SharePoint On-Premises Using Python</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
