<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Jason Niver's Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/jasonn/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/jasonn/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-05-13T14:39:00Z</updated><entry><title>SQL Server Index Maintenance</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/archive/2008/11/04/sql-server-index-maintenance.aspx" /><id>http://blogs.msdn.com/jasonn/archive/2008/11/04/sql-server-index-maintenance.aspx</id><published>2008-11-04T23:09:17Z</published><updated>2008-11-04T23:09:17Z</updated><content type="html">&lt;p&gt;After working on a problem where the transaction logs for a large database were filling to sizes larger then the actual database every night it turned out to be a server maintenance plan that rebuilt and reorganized indexes every night.&amp;nbsp; While working on designing a custom plan that would only rebuild or reorganize when actually necessary I found many references out on the internet.&amp;nbsp; None of the scripts I found out there really accomplished what I was after.&lt;/p&gt; &lt;p&gt;Starting with finding the indexes that need maintenance done on them I eventually came up with this SQL:&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;    sch.name [schema_name]&lt;/pre&gt;&lt;pre class="alt"&gt;    ,obj.name [table_name]&lt;/pre&gt;&lt;pre&gt;    ,idx.name [index_name]&lt;/pre&gt;&lt;pre class="alt"&gt;    ,idx.type_desc [index_type]&lt;/pre&gt;&lt;pre&gt;    ,part.used_page_count*8 [used_kb] --&lt;span class="kwrd"&gt;each&lt;/span&gt; page &lt;span class="kwrd"&gt;is&lt;/span&gt; 8k&lt;/pre&gt;&lt;pre class="alt"&gt;    ,part.row_count&lt;/pre&gt;&lt;pre&gt;    ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;DECIMAL&lt;/span&gt;(9,2),frag.avg_fragmentation_in_percent) [fragmentation]&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;from&lt;/span&gt; sys.dm_db_partition_stats part&lt;/pre&gt;&lt;pre&gt;        &lt;span class="kwrd"&gt;join&lt;/span&gt; sys.objects obj &lt;span class="kwrd"&gt;on&lt;/span&gt; part.object_id = obj.object_id&lt;/pre&gt;&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;join&lt;/span&gt; sys.schemas sch &lt;span class="kwrd"&gt;on&lt;/span&gt; obj.schema_id = sch.schema_id&lt;/pre&gt;&lt;pre&gt;        &lt;span class="kwrd"&gt;left&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt; sys.indexes idx &lt;span class="kwrd"&gt;on&lt;/span&gt; part.object_id = idx.object_id &lt;span class="kwrd"&gt;and&lt;/span&gt; part.index_id = idx.index_id&lt;/pre&gt;&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;left&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt; sys.dm_db_index_physical_stats(db_id(),&lt;span class="kwrd"&gt;null&lt;/span&gt;,&lt;span class="kwrd"&gt;null&lt;/span&gt;,&lt;span class="kwrd"&gt;null&lt;/span&gt;,&lt;span class="kwrd"&gt;null&lt;/span&gt;) frag &lt;span class="kwrd"&gt;on&lt;/span&gt; part.object_id = frag.object_id&lt;/pre&gt;&lt;pre&gt;            &lt;span class="kwrd"&gt;and&lt;/span&gt; part.index_id = frag.index_id&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;where&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;        obj.is_ms_shipped = 0&lt;/pre&gt;&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;and&lt;/span&gt; frag.page_count &amp;gt; 100&lt;/pre&gt;&lt;pre&gt;    &lt;span class="kwrd"&gt;order&lt;/span&gt; &lt;span class="kwrd"&gt;by&lt;/span&gt; [schema_name],[table_name],[index_name];&lt;/pre&gt;&lt;pre&gt;&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This gave me a nice view to see how big the tables were, and where the fragmentation problems were.&amp;nbsp; We decided that if it's less800KB (based on frag.page_count) we didn't care about the fragmentation.&lt;/p&gt;
&lt;p&gt;Armed with this info it was a matter of basically executing the rebuild or reorganize depending on your preference.&amp;nbsp; I decided to use a in memory table and just did a insert-select into it.&amp;nbsp; My temp table looked like:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @IndexStatus &lt;span class="kwrd"&gt;table&lt;/span&gt;(&lt;/pre&gt;&lt;pre&gt;    schema_name &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(250)&lt;/pre&gt;&lt;pre class="alt"&gt;    ,table_name &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(250)&lt;/pre&gt;&lt;pre&gt;    ,index_name &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(250)&lt;/pre&gt;&lt;pre class="alt"&gt;    ,index_type &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(250)&lt;/pre&gt;&lt;pre&gt;    ,used_kb &lt;span class="kwrd"&gt;int&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;    ,row_count &lt;span class="kwrd"&gt;int&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;    ,fragmentation &lt;span class="kwrd"&gt;decimal&lt;/span&gt;(9,2));&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;
&lt;p&gt;From there I just took the results into a cursor and executed dynamic sql.&amp;nbsp; My logic was &amp;lt;10% fragmentation I didn't care, greater then 30 gets a rebuild, 10-30 gets a reorganize&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; maintCursor &lt;span class="kwrd"&gt;cursor&lt;/span&gt; &lt;span class="kwrd"&gt;for&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;    &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;'alter index ['&lt;/span&gt;+[index_name]+&lt;span class="str"&gt;'] on ['&lt;/span&gt;+[schema_name]+&lt;span class="str"&gt;'].['&lt;/span&gt;+[table_name]+&lt;span class="str"&gt;']'&lt;/span&gt;, fragmentation &lt;/pre&gt;&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;from&lt;/span&gt; @IndexStatus &lt;/pre&gt;&lt;pre&gt;        &lt;span class="kwrd"&gt;where&lt;/span&gt; fragmentation &amp;gt; 10;&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @stmt &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(&lt;span class="kwrd"&gt;max&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @frag &lt;span class="kwrd"&gt;float&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;open&lt;/span&gt; maintCursor&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;fetch&lt;/span&gt; &lt;span class="kwrd"&gt;next&lt;/span&gt; &lt;span class="kwrd"&gt;from&lt;/span&gt; maintCursor &lt;span class="kwrd"&gt;into&lt;/span&gt; @stmt, @frag&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;while&lt;/span&gt; &lt;span class="preproc"&gt;@@fetch_status&lt;/span&gt; = 0&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;begin&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; @frag &amp;gt; 30&lt;/pre&gt;&lt;pre&gt;        &lt;span class="kwrd"&gt;set&lt;/span&gt; @stmt = @stmt+&lt;span class="str"&gt;' rebuild'&lt;/span&gt;; --&lt;span class="kwrd"&gt;if&lt;/span&gt; this &lt;span class="kwrd"&gt;is&lt;/span&gt; enterprise you can &lt;span class="kwrd"&gt;add&lt;/span&gt; &lt;span class="str"&gt;'with (online=on)'&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;        &lt;span class="kwrd"&gt;set&lt;/span&gt; @stmt = @stmt+&lt;span class="str"&gt;' reorganize'&lt;/span&gt;;&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;/pre&gt;&lt;pre&gt;    &lt;span class="kwrd"&gt;print&lt;/span&gt; (@stmt)&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;exec&lt;/span&gt; (@stmt)&lt;/pre&gt;&lt;pre&gt;    &lt;span class="kwrd"&gt;fetch&lt;/span&gt; &lt;span class="kwrd"&gt;next&lt;/span&gt; &lt;span class="kwrd"&gt;from&lt;/span&gt; maintCursor &lt;span class="kwrd"&gt;into&lt;/span&gt; @stmt, @frag&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;close&lt;/span&gt; maintCursor&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;deallocate&lt;/span&gt; maintCursor&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9041209" width="1" height="1"&gt;</content><author><name>janiver@microsoft.com</name><uri>http://blogs.msdn.com/members/janiver%40microsoft.com.aspx</uri></author></entry><entry><title>Sending Encrypted E-Mails in C#</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/archive/2008/08/18/sending-encrypted-e-mails-in-c.aspx" /><id>http://blogs.msdn.com/jasonn/archive/2008/08/18/sending-encrypted-e-mails-in-c.aspx</id><published>2008-08-19T01:14:26Z</published><updated>2008-08-19T01:14:26Z</updated><content type="html">&lt;p&gt;So I was faced with the problem of sending an encrypted email to a group of people.&amp;nbsp; Not really thinking, I responded with sure we can do that no problem. Getting back to my desk I started working on the project to find out that it turns out to be more difficult then I had thought.&lt;/p&gt; &lt;p&gt;Doing some internet searches, I found several commercial products that allow you to encrypt emails.&amp;nbsp; I also found several discussions where people got close to what they were after, most were just after digital signatures without attachments.&amp;nbsp; Nothing close to a full package.&lt;/p&gt; &lt;p&gt;After a few hours worth of digging I finally was able to come up with a solution that met my needs, multiple addressees and multiple attachments.&amp;nbsp; Below is the code, note that you need to add a reference to System.Security in your Visual Studio Project to compile this code.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt; &lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;  &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Net.Mail;&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.IO;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Cryptography.Pkcs;&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Cryptography.X509Certificates;&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CommonUtilities&lt;/pre&gt;&lt;pre class="alt"&gt;{&lt;/pre&gt;&lt;pre&gt;    &lt;span class="rem"&gt;//requires reference to System.Security&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; EmailUtil&lt;/pre&gt;&lt;pre&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendEncryptedEmail(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] to, &lt;span class="kwrd"&gt;string&lt;/span&gt; from, &lt;span class="kwrd"&gt;string&lt;/span&gt; subject, &lt;span class="kwrd"&gt;string&lt;/span&gt; body, &lt;span class="kwrd"&gt;string&lt;/span&gt;[] attachments)&lt;/pre&gt;&lt;pre&gt;        {&lt;/pre&gt;&lt;pre class="alt"&gt;            MailMessage message = &lt;span class="kwrd"&gt;new&lt;/span&gt; MailMessage();&lt;/pre&gt;&lt;pre&gt;            message.From = &lt;span class="kwrd"&gt;new&lt;/span&gt; MailAddress(from);&lt;/pre&gt;&lt;pre class="alt"&gt;            message.Subject = subject;&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (attachments != &lt;span class="kwrd"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; attachments.Length &amp;gt; 0)&lt;/pre&gt;&lt;pre&gt;            {&lt;/pre&gt;&lt;pre class="alt"&gt;                StringBuilder buffer = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder();&lt;/pre&gt;&lt;pre&gt;                buffer.Append(&lt;span class="str"&gt;"MIME-Version: 1.0\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                buffer.Append(&lt;span class="str"&gt;"Content-Type: multipart/mixed; boundary=unique-boundary-1\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                buffer.Append(&lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                buffer.Append(&lt;span class="str"&gt;"This is a multi-part message in MIME format.\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                buffer.Append(&lt;span class="str"&gt;"--unique-boundary-1\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                buffer.Append(&lt;span class="str"&gt;"Content-Type: text/plain\r\n"&lt;/span&gt;);  &lt;span class="rem"&gt;//could use text/html as well here if you want a html message&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;                buffer.Append(&lt;span class="str"&gt;"Content-Transfer-Encoding: 7Bit\r\n\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                buffer.Append(body);&lt;/pre&gt;&lt;pre&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (!body.EndsWith(&lt;span class="str"&gt;"\r\n"&lt;/span&gt;))&lt;/pre&gt;&lt;pre class="alt"&gt;                    buffer.Append(&lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                buffer.Append(&lt;span class="str"&gt;"\r\n\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; filename &lt;span class="kwrd"&gt;in&lt;/span&gt; attachments)&lt;/pre&gt;&lt;pre class="alt"&gt;                {&lt;/pre&gt;&lt;pre&gt;                    FileInfo fileInfo = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(filename);&lt;/pre&gt;&lt;pre class="alt"&gt;                    buffer.Append(&lt;span class="str"&gt;"--unique-boundary-1\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                    buffer.Append(&lt;span class="str"&gt;"Content-Type: application/octet-stream; file="&lt;/span&gt; + fileInfo.Name + &lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                    buffer.Append(&lt;span class="str"&gt;"Content-Transfer-Encoding: base64\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                    buffer.Append(&lt;span class="str"&gt;"Content-Disposition: attachment; filename="&lt;/span&gt; + fileInfo.Name + &lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                    buffer.Append(&lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                    &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] binaryData = File.ReadAllBytes(filename);&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;                    &lt;span class="kwrd"&gt;string&lt;/span&gt; base64Value = Convert.ToBase64String(binaryData, 0, binaryData.Length);&lt;/pre&gt;&lt;pre class="alt"&gt;                    &lt;span class="kwrd"&gt;int&lt;/span&gt; position = 0;&lt;/pre&gt;&lt;pre&gt;                    &lt;span class="kwrd"&gt;while&lt;/span&gt; (position &amp;lt; base64Value.Length)&lt;/pre&gt;&lt;pre class="alt"&gt;                    {&lt;/pre&gt;&lt;pre&gt;                        &lt;span class="kwrd"&gt;int&lt;/span&gt; chunkSize = 100;&lt;/pre&gt;&lt;pre class="alt"&gt;                        &lt;span class="kwrd"&gt;if&lt;/span&gt; (base64Value.Length - (position + chunkSize) &amp;lt; 0)&lt;/pre&gt;&lt;pre&gt;                            chunkSize = base64Value.Length - position;&lt;/pre&gt;&lt;pre class="alt"&gt;                        buffer.Append(base64Value.Substring(position, chunkSize));&lt;/pre&gt;&lt;pre&gt;                        buffer.Append(&lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;                        position += chunkSize;&lt;/pre&gt;&lt;pre&gt;                    }&lt;/pre&gt;&lt;pre class="alt"&gt;                    buffer.Append(&lt;span class="str"&gt;"\r\n"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;                }&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;                body = buffer.ToString();&lt;/pre&gt;&lt;pre class="alt"&gt;            }&lt;/pre&gt;&lt;pre&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;            {&lt;/pre&gt;&lt;pre&gt;                body = &lt;span class="str"&gt;"Content-Type: text/plain\r\nContent-Transfer-Encoding: 7Bit\r\n\r\n"&lt;/span&gt; + body;&lt;/pre&gt;&lt;pre class="alt"&gt;            }&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] messageData = Encoding.ASCII.GetBytes(body);&lt;/pre&gt;&lt;pre&gt;            ContentInfo content = &lt;span class="kwrd"&gt;new&lt;/span&gt; ContentInfo(messageData);&lt;/pre&gt;&lt;pre class="alt"&gt;            EnvelopedCms envelopedCms = &lt;span class="kwrd"&gt;new&lt;/span&gt; EnvelopedCms(content);&lt;/pre&gt;&lt;pre&gt;            CmsRecipientCollection toCollection = &lt;span class="kwrd"&gt;new&lt;/span&gt; CmsRecipientCollection();&lt;/pre&gt;&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; address &lt;span class="kwrd"&gt;in&lt;/span&gt; to)&lt;/pre&gt;&lt;pre&gt;            {&lt;/pre&gt;&lt;pre class="alt"&gt;                message.To.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; MailAddress(address));&lt;/pre&gt;&lt;pre&gt;                X509Certificate2 certificate = &lt;span class="kwrd"&gt;null&lt;/span&gt;; &lt;span class="rem"&gt;//Need to load from store or from file the client's cert&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;                CmsRecipient recipient = &lt;span class="kwrd"&gt;new&lt;/span&gt; CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, certificate);&lt;/pre&gt;&lt;pre&gt;                toCollection.Add(recipient);&lt;/pre&gt;&lt;pre class="alt"&gt;            }&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;            envelopedCms.Encrypt(toCollection);&lt;/pre&gt;&lt;pre&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] encryptedBytes = envelopedCms.Encode();&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;            &lt;span class="rem"&gt;//add digital signature:&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;            SignedCms signedCms = &lt;span class="kwrd"&gt;new&lt;/span&gt; SignedCms(&lt;span class="kwrd"&gt;new&lt;/span&gt; ContentInfo(encryptedBytes));&lt;/pre&gt;&lt;pre&gt;            X509Certificate2 signerCertificate = &lt;span class="kwrd"&gt;null&lt;/span&gt;; &lt;span class="rem"&gt;//Need to load from store or from file the signer's cert&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;            CmsSigner signer = &lt;span class="kwrd"&gt;new&lt;/span&gt; CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, signerCertificate);&lt;/pre&gt;&lt;pre&gt;            signedCms.ComputeSignature(signer);&lt;/pre&gt;&lt;pre class="alt"&gt;            encryptedBytes = signedCms.Encode();&lt;/pre&gt;&lt;pre&gt;            &lt;span class="rem"&gt;//end digital signature section&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;            MemoryStream stream = &lt;span class="kwrd"&gt;new&lt;/span&gt; MemoryStream(encryptedBytes);&lt;/pre&gt;&lt;pre class="alt"&gt;            AlternateView view = &lt;span class="kwrd"&gt;new&lt;/span&gt; AlternateView(stream, &lt;span class="str"&gt;"application/pkcs7-mime; smime-type=signed-data;name=smime.p7m"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;            message.AlternateViews.Add(view);&lt;/pre&gt;&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;            SmtpClient client = &lt;span class="kwrd"&gt;new&lt;/span&gt; SmtpClient(&lt;span class="str"&gt;"your.smtp.mailhost"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;            &lt;span class="rem"&gt;//add authentication info if required by your smtp server etc...&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;            &lt;span class="rem"&gt;//client.Credentials = CredentialCache.DefaultCredentials;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;            client.Send(message);&lt;/pre&gt;&lt;pre&gt;        }&lt;/pre&gt;&lt;pre class="alt"&gt;    }&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This should get you pretty much everything you need. The only thing left is to load the certificates from somewhere.&amp;nbsp; I used a function to load them from the current users certificate store on the machine.&amp;nbsp; I have a previous blog posting on &lt;a href="http://blogs.msdn.com/jasonn/archive/2008/07/16/using-certificates-from-the-windows-certificate-store.aspx"&gt;how to load the certificates from the store&lt;/a&gt;.&amp;nbsp; The trick comes in to fetching the certificates for unknown parties etc.&amp;nbsp; &lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8877336" width="1" height="1"&gt;</content><author><name>janiver@microsoft.com</name><uri>http://blogs.msdn.com/members/janiver%40microsoft.com.aspx</uri></author><category term="C#" scheme="http://blogs.msdn.com/jasonn/archive/tags/C_2300_/default.aspx" /><category term="Security" scheme="http://blogs.msdn.com/jasonn/archive/tags/Security/default.aspx" /></entry><entry><title>Using certificates from the Windows certificate store</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/archive/2008/07/16/using-certificates-from-the-windows-certificate-store.aspx" /><id>http://blogs.msdn.com/jasonn/archive/2008/07/16/using-certificates-from-the-windows-certificate-store.aspx</id><published>2008-07-16T21:15:00Z</published><updated>2008-07-16T21:15:00Z</updated><content type="html">&lt;p&gt;I recently had to setup some web services that used certificates to communicate back and forth and one thing I found is that pretty much every site I found references on was using a file on the file system to access the client certificate.&amp;nbsp; What we wanted to do was access the certificates directly from the store.&amp;nbsp; After a little playing around we got it to work, so I though I would put this out in case someone else wants to do the same.&amp;nbsp; There is another X509Certificate2Collection item at MSDN, however the one for the X509Certificate2 only shows getting the file.&lt;/p&gt;&lt;p&gt;Assuming:&lt;br&gt;using System.Security.Cryptography.X509Certificates;&lt;/p&gt;&lt;p&gt;private X509Certificate GetCertificate()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; store.Open(OpenFlags.ReadOnly);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySerialNumber, "123456", true);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; store.Close();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return certs[0];&lt;br&gt;} &lt;br&gt;&lt;/p&gt;&lt;p&gt;There are several ways to do the search, I like the FindBySerialNumber, but you can also search by DN, SubjectName, etc.&amp;nbsp;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8739676" width="1" height="1"&gt;</content><author><name>janiver@microsoft.com</name><uri>http://blogs.msdn.com/members/janiver%40microsoft.com.aspx</uri></author><category term="C#" scheme="http://blogs.msdn.com/jasonn/archive/tags/C_2300_/default.aspx" /></entry><entry><title>Downloading files from the internet in PowerShell (with progress)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/archive/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress.aspx" /><id>http://blogs.msdn.com/jasonn/archive/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress.aspx</id><published>2008-06-13T16:42:00Z</published><updated>2008-06-13T16:42:00Z</updated><content type="html">&lt;P&gt;Ok, so the &lt;SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;easiest &lt;/SPAN&gt;way (that I know of) to download files in powershell from the internet is to use the .net WebClient.&amp;nbsp; The simple way I started with was the two liner:&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#800080&gt;$client&lt;/FONT&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;SPAN style="COLOR: #5f9ea0"&gt;New-Object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"System.Net.WebClient"&lt;BR&gt;&lt;FONT color=#800080&gt;$client.DownloadFile("http://somesite.com/largefile.zip","c:\temp\largefile.zip"&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="COLOR: #000000"&gt;&lt;SPAN style="COLOR: #800000"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;However I was working on a script that required some pretty large files to be downloaded, and using the DownloadFile method has no progress indicator.&amp;nbsp; I figured some users might thing the program died.&amp;nbsp; So I decided to create a method that still uses the webclient to download the files, however give a status of where it is in the download.&amp;nbsp; Here is what I came up with, hopefully someone else will find it useful.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;downloadFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$url&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$targetFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;BR&gt;{ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Downloading $url"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $uri&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #5f9ea0"&gt;New-Object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"System.Uri"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"$url"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $request&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;System.Net.HttpWebRequest&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;::&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Create&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$uri&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $request&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;set_Timeout&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;15000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;#15 second timeout &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $response&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$request&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;GetResponse&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $totalLength&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;System.Math&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;::&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Floor&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$response&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;get_ContentLength&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;()&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1024&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $responseStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$response&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;GetResponseStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $targetStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #5f9ea0"&gt;New-Object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #5f9ea0"&gt;-TypeName&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;System.IO.FileStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #5f9ea0"&gt;-ArgumentList&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$targetFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;Create&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $buffer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #5f9ea0"&gt;new-object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;byte&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;[] &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;10&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;KB&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $count&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$responseStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Read&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$buffer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$buffer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;length&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $downloadedBytes&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$count&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;while&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$count&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;-gt&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;System.Console&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;::&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;CursorLeft&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;System.Console&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;::&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Write&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"Downloaded {0}K of {1}K"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;System.Math&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;::&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Floor&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$downloadedBytes&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1024&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$totalLength&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $targetStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Write&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$buffer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$count&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $count&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$responseStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Read&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$buffer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$buffer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;length&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $downloadedBytes&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$downloadedBytes&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$count&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "`nFinished Download"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $targetStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Flush&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;()&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN class=style1&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$targetStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Close&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$targetStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Dispose&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;$responseStream&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #8b4513"&gt;Dispose&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() &lt;BR&gt;} &lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="COLOR: #000000"&gt;This would be used like&lt;BR&gt;downloadFile "http://somesite/largefile.zip" "c:\temp\largefile.zip"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="COLOR: #000000"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8594493" width="1" height="1"&gt;</content><author><name>janiver@microsoft.com</name><uri>http://blogs.msdn.com/members/janiver%40microsoft.com.aspx</uri></author><category term="PowerShell" scheme="http://blogs.msdn.com/jasonn/archive/tags/PowerShell/default.aspx" /></entry><entry><title>Importing a File Share into Document Library via PowerShell</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/archive/2008/05/22/importing-a-file-share-into-document-library-via-powershell.aspx" /><id>http://blogs.msdn.com/jasonn/archive/2008/05/22/importing-a-file-share-into-document-library-via-powershell.aspx</id><published>2008-05-22T04:24:53Z</published><updated>2008-05-22T04:24:53Z</updated><content type="html">&lt;p&gt;So today I was asked for a way to automate importing files from a file share into a document library.&amp;#160; For this, ACL's were not an issue, however using PS's Get-ACL function can get you that as well to match the insert.&lt;/p&gt;  &lt;p&gt;Here is what I came up with as a quick and dirty response.&amp;#160; I though it might be nice to post for other people to use as a base script for things like synchronizing file shares with document libraries etc.&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;#Variables&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;$targetLibrary = &lt;span style="color: #006080"&gt;&amp;quot;Imported Documents&amp;quot;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;$sourceDirectory = &lt;span style="color: #006080"&gt;&amp;quot;\\somemachine\someshare&amp;quot;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;$siteUrl = &lt;span style="color: #006080"&gt;&amp;quot;http://localhost/sites/somesite&amp;quot;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;#open the SPWeb/SPSite&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;[System.Reflection.Assembly]::LoadWithPartialName(&lt;span style="color: #006080"&gt;&amp;quot;Microsoft.SharePoint&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;$site = New-Object &lt;span style="color: #006080"&gt;&amp;quot;Microsoft.SharePoint.SPSite&amp;quot;&lt;/span&gt; $siteUrl&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;$web = $site.OpenWeb()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;#get all the files and directories on the share and iterate them&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; ($file &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; get-childitem $sourceDirectory -recurse)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    $fileName = ($file | Select-Object Name).Name    #get the &lt;span style="color: #0000ff"&gt;base&lt;/span&gt; file nname&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    $parentPath = ($file | Select-Object PSParentPath) #get the path&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    #eliminate everything &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; the path prior to the root folder&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    $parentPath = $parentPath.Substring($parentPath.PSParentPath.ToLower().IndexOf($sourceDirectory.ToLower())+$sourceDirectory.Length)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    $parentPath = $parentPath.Replace(&lt;span style="color: #006080"&gt;&amp;quot;\&amp;quot;,&amp;quot;&lt;/span&gt;/&lt;span style="color: #006080"&gt;&amp;quot;)    #make the path url friendly&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    $targetPath = &amp;quot;&lt;/span&gt;/$targetLibrary$parentPath/$fileName&lt;span style="color: #006080"&gt;&amp;quot; #combine for a full url&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    #check if this is a directory or file&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    if (($file.GetType() | Select-Object Name).Name -eq &amp;quot;&lt;/span&gt;DirectoryInfo&lt;span style="color: #006080"&gt;&amp;quot;)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;        &amp;quot;&lt;/span&gt;Adding Folder: $targetPath&lt;span style="color: #006080"&gt;&amp;quot;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;        $web.get_Folders().Add($targetPath)    &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    else&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;        $parentFile = ($file | Select-Object FullName).FullName #gets the full path to the file&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;        &amp;quot;&lt;/span&gt;Adding File: $targetPath&lt;span style="color: #006080"&gt;&amp;quot;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;        $fileBytes = [System.IO.File]::OpenRead(&amp;quot;&lt;/span&gt;$parentFile&amp;quot;) #read the file to a &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt; arry&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;        $web.get_Files().Add($targetPath,$fileBytes)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;#dispose of the web/site&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;$web.Dispose()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;$site.Dispose()&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8530821" width="1" height="1"&gt;</content><author><name>janiver@microsoft.com</name><uri>http://blogs.msdn.com/members/janiver%40microsoft.com.aspx</uri></author><category term="SharePoint" scheme="http://blogs.msdn.com/jasonn/archive/tags/SharePoint/default.aspx" /><category term="PowerShell" scheme="http://blogs.msdn.com/jasonn/archive/tags/PowerShell/default.aspx" /></entry><entry><title>Web Parts the Simple Way - Use User Controls</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/jasonn/archive/2008/05/13/web-parts-the-simple-way-use-user-controls.aspx" /><id>http://blogs.msdn.com/jasonn/archive/2008/05/13/web-parts-the-simple-way-use-user-controls.aspx</id><published>2008-05-13T16:39:00Z</published><updated>2008-05-13T16:39:00Z</updated><content type="html">&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;One of the first questions I usually get about SharePoint development is how to make the WebPart development process simpler.&amp;nbsp; My answer to that is use UserControls.&amp;nbsp; There is a very good solution on CodePlex called Smart Parts &lt;A href="http://www.codeplex.com/smartpart" mce_href="http://www.codeplex.com/smartpart"&gt;http://www.codeplex.com/smartpart&lt;/A&gt;&amp;nbsp; However I have found in some environments the customer does not want to add a third party product into the mix.&amp;nbsp; In these cases the fastest way I have found to create webparts is to use user controls and wrap them with the webpart.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;We start with a very simple WebPart class that only overrides the CreateChildControls method, we use this to load our user control.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InboxListWebPart&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;WebPart&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; control = &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CreateChildControls()&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Controls.Clear();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; control = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Page.LoadControl(&lt;SPAN style="COLOR: #a31515"&gt;"/_controltemplates/MyCustomControls/InboxListControl.ascx"&lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Controls.Add(control);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;From there everything is driven from my usercontrol InboxListControl.ascx.&amp;nbsp; The control goes into the program files\common files\microsoft shared\web server extensions\12\templates\controltemplates directory (as shown above I created a sub directory called MyCustomControls).&amp;nbsp; I usually will deploy the code behind into the GAC, but they can be deployed into the same directory as well.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;If you need to do more advanced webpart stuff (like connections or properties) you can get to your webpart from your user control.&amp;nbsp; For example if I had a property that I wanted to present to the user (say like Inbox Name) in my .cs file for the webpart I would do the following (using the 3.5 property syntax):&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;Personalizable&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;PersonalizationScope&lt;/SPAN&gt;.User), &lt;SPAN style="COLOR: #2b91af"&gt;WebBrowsable&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;WebDisplayName&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Inbox Name"&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;WebDescription&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"some useful description"&lt;/SPAN&gt;)]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; InboxName&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;In my user control I usually define a parent webpart property that returns the webpart I used to wrap my user control:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;private&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InboxListWebPart&lt;/SPAN&gt; ParentWebPart&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Parent &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InboxListWebPart&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;From here I can now define my InboxName on my user control:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt; InboxName&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; ParentWebPart.InboxName;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ParentWebPart.InboxName = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;It takes a little more code when you want to do things like properties and connections, but I think the usability of having your UI in an ascx file more then makes up for this small inconvenience.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8500342" width="1" height="1"&gt;</content><author><name>janiver@microsoft.com</name><uri>http://blogs.msdn.com/members/janiver%40microsoft.com.aspx</uri></author><category term="SharePoint" scheme="http://blogs.msdn.com/jasonn/archive/tags/SharePoint/default.aspx" /></entry></feed>