How to Resize a VirtualBox Disk in Windows

If you created your VirtualBox .vdi disk too small, there is a simple utility to increase the size of the disk. 

  1. Leave VirtualBox open, but stop the VM
  2. Open a command prompt (don’t use an Administrative command prompt unless VirtualBox is running as Administrator)
  3. Use the VBoxManage.exe application to set the new size in MB

Here is an example resizing to ~70gb.

cd c:\Program Files\Oracle\VirtualBox

VBoxManage.exe modifyhd "c:\path\to\image.vdi" --resize 70000

You’ll then need to start your VM and resize your partition (or create a new partition) to take advantage of the space.

If you get an error that looks like the below then vboxmanage.exe was unable to communicate with the VirtualBox application.  Try changing your command prompt (either to an Administrator prompt or a non-elevated prompt) and restarting the VirtualBox application.

VBoxManage.exe: error: Failed to create the VirtualBox object!
VBoxManage.exe: error: Code CO_E_SERVER_EXEC_FAILURE (0x80080005) - Server execution failed (extended info not available)

VBoxManage.exe: error: Most likely, the VirtualBox COM server is not running or failed to start.

Simple AVR Button Debounce

I needed a simple debounce for a push button wired directly to a digital input on an ATMega.  I’m sure there are more advanced techniques but this is working well for me.

int ButtonPressed()
{
 /* the button is pressed when BUTTON_BIT is clear */
 if (bit_is_clear(BUTTON_PIN, BUTTON_BIT))
 {
  delay_ms(DEBOUNCE_TIME);
  if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) return 1;
 }
 return 0;
}

Where bit_is_clear is defined in AVR-LIBC IO.H as

#define bit_is_clear ( sfr, bit ) (!(_SFR_BYTE(sfr) & _BV(bit)))

and when I actually use this to detect a press I incorporate a timer to prevent another detection (double press).  The timer is started and a blocking flag cleared upon completion, which will then allow a second detection.

BOOL buttonWaiting = FALSE;
int buttonCounter = 0;

void ProcessCurrentState()
{
 // Check buttons here
 if (!buttonWaiting && ButtonPressed())
 { 
  buttonWaiting = TRUE;
  buttonCounter = 0;

  timerAttach(TIMER2OVERFLOW_INT, ClearButtonWait);

  // Process button press
  // or take appropiate action
 }
}

void ClearButtonWait()
{
 buttonCounter++;
 if(buttonCounter > BUTTON_OVERFLOW_COUNT)
 {
  buttonWaiting = FALSE;
  timerDetach(TIMER2OVERFLOW_INT);
 }
}

 

 

Procyon AVRlib Archive

If you have done any Atmel / AVR development you have probably heard of Procyon AVRLib.  If you haven’t, you are in for a treat.  It is an old library, but contains incredibly valuable drivers for all manner of popular hardware and tasks.

AVRLib was written by Pascal Stang and he maintains a site here.  The problem is that the site is not well maintained and often the library is completely unavailable.

I’m going to go ahead and maintain a mirror of the latest library package which you can download here at any time: Procyon AVRlib

Programmatically Activating SharePoint Features

SharePoint features can exist at several different scopes, including Web, Site, and Web Application.  Each of these can be activated and deactivated programmatically, but they have to be handled slightly differently.

 

This can be extremely useful for automated deployment tools, or any number of other purposes.

 

Given a WebApplication scoped feature, you’ll need an SPWebApplication object (which you can get from the property on your SPSite object if needed):

 

For example, if you wanted a method which could activate a WebApplication scoped feature, an optionally deactivate and reactivate (useful after upgrading the feature during a deployment):

 

        private void ActivateWebApplicationFeature(SPWebApplication WebApplication, Guid FeatureGuid, bool Reactivate)
        {
            if (Reactivate)
            {
                try
                {
                    //Attempt to deactivate feature.  Will fail if already deactivate.
                    WebApplication.Features.Remove(FeatureGuid, true);
                }
                catch
                {
                    //Was not activated, or could not be deactivated.
                }
            }

            //Now activate
            WebApplication.Features.Add(FeatureGuid);
        }
 

For a feature scoped at the Site level, you simply leverage the SPSite object

{SPSite}.Features.Add(FeatureGuid);

{SPSite}.Features.Remove(FeatureGuid, true);

 

And for the Web scoped features, of course, just use the SPWeb

{SPWeb}.Features.Add(FeatureGuid);

{SPWeb}.Features.Remove(FeatureGuid, true);

 

The boolean on the remove indicates whether to force deactivation.  Note that if the feature ID can not be found, permissions are not available, or it is already in the requested state, you will get a failure – So be sure to wrap your code with the appropriate error handling.

 

 

http://findreviews.me

SharePoint Designer Zip/Compress Workflow Action Deployed in a Sandboxed Solution

Whew, that was a long title – But I think you’ll find this very useful.  Some notes on deploying a custom workflow action that you can use inside of SharePoint designer, which will Zip attachments to list items and deposit the compressed file in a document library – and it is all deployed in a sandboxed solution, so you can use it on hosted SharePoint Foundation instances.  In this case, I even generate PDF files with the data from the list items, and include them in the compressed file.  Great for archival or offsite storage.

 

First of all, this MSDN article about general workflow actions and sandboxed solutions will help you immensely with the basics:

http://msdn.microsoft.com/en-us/library/gg615449.aspx

Next you’ll want to grab a copy of:

  • SharpZipLib (an open source compression library)
  • SharpPDF (if you want to generate PDF’s too)

You’ll want to get the source to each, as we’ll upgrade them to .net 4.0, and build them as signed assemblies.

 

Make yourself a SharePoint solution in Visual Studio per the MSDN article above, and add two (or one if you are leaving out the PDF bits) existing projects using the source of the libraries (SharpZip, SharpPDF). 

  • You’ll need to let the upgrade wizard upgrade them to .net 4.
  • Check the project settings on them and make sure they are targeting the .net 4 framework (AnyCPU)
  • For SharpPDF you’ll need to open the AssemblyInfo.cs file and add a line to the bottom
[assembly: AllowPartiallyTrustedCallers()]

 

In my case, I’m always looking at a single list, so my workflow action only accepts a ListItemID.  You’ll probably want to make that more generic by adding a few more parameters to your Action.  Again, these custom actions are described in plenty of detail in the MSDN article referenced above.

 

       public static Hashtable Package(SPUserCodeWorkflowContext context, string ItemID)
        {
            Hashtable result = new Hashtable();

            try
            {
                result["Result"] = PackageListItem(closingId, context.CurrentWebUrl);
            }
            catch (Exception e)
            {
                result["Result"] = "ERROR" + e.ToString();
            }

            return result;
        }

 

Where the PackageListItem method looks like this:

 

        public static string PackageListItem(string ItemID, string webUrl)
        {
            SortedList<string, byte[]> files = new SortedList<string,byte[]>();
            
            using (SPSite site = new SPSite(webUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //Find the list item
                    SPListItem item = web.Lists["List"].GetItemById(int.Parse(ItemID));

                    //Create a PDF of the list item data and add it to the 
                    //  compressed files collection
                    files.Add("ListData.pdf", CreateDocument(item, "Title"));

                    //Loop through all list item attachments
                    //  and add them to the compressed file collection
                    foreach (string attachment in item.Attachments)
                    {
                        SPFile attFile = web.GetFile(item.Attachments.UrlPrefix + attachment);
                        files.Add(attachment, attFile.OpenBinary());
                    }
                    
                    //Zip everything up and finish!
                    byte[] package = CreateZip(files);

                    files = null;

                    web.AllowUnsafeUpdates = true;

                    //Add the zipped package to a doc lib
                    SPFile file = web.Folders["Archive"].Files.Add(
                        string.Format("({0})_{1}.zip", 
                        ItemID, 
                        DateTime.Now.ToString("yyyy'-'MM'-'dd HH''mm''ss")), 
                        package);

                    web.Update();

                    file.Update();

                    web.AllowUnsafeUpdates = false;

                    package = null;

                    //return the List Item ID of the generated file to the workflow
                    return file.ListItemAllFields["ID"].ToString();

                }
            }
        }

 

And create document, if you are interested in generating PDF’s, looks like this.  I do some hackery to leave out boring version and guid fields from the list data – feel free to do something more intelligent:

 

 

 public static byte[] CreateDocument(SPListItem item, string type)
        {
            pdfDocument pdf = new pdfDocument(type, "Some Text");
            pdfPage page = pdf.addPage();

            page.addText(item["Title"].ToString(), 20, 730, pdf.getFontReference(predefinedFont.csHelvetica), 12);

            pdfTable table = new pdfTable(pdf, 1, pdfColor.Black);
            table.borderSize = 1;
            table.borderColor = pdfColor.Black;
            table.coordX = 50;
            table.coordY = 690;


            table.tableHeader.addColumn(150);
            table.tableHeader.addColumn(300);

            foreach (SPField f in item.Fields)
            {
                if (f.Type != SPFieldType.Guid && !f.Title.ToLower().Contains("version") && !f.Title.Contains("Title"))
                {
                    try
                    {
                        pdfTableRow row = table.createRow();
                        row[0].addText(f.Title);
                        row[1].addText(item[f.InternalName].ToString());
                        table.addRow(row);
                    }
                    catch { }
                }
            }
            
            page.addTable(table);

            MemoryStream ms = new MemoryStream();
            pdf.createPDF(ms);

            return ms.ToArray();
        }

 

The CreateZip method called in package is also very simple:

 

 public static byte[] CreateZip(SortedList<string, byte[]> files)
        {

            MemoryStream ms = new MemoryStream();

            using (ZipOutputStream s = new ZipOutputStream(ms))
            {

                s.SetLevel(5); // 0 - store only to 9 - means best compression

                foreach (string file in files.Keys)
                {

                    // Using GetFileName makes the result compatible with XP
                    // as the resulting path is not absolute.
                    ZipEntry entry = new ZipEntry(file);

                    // Could also use the last write time or similar for the file.
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);

                    s.Write(files[file], 0, files[file].Length);                    
                }

            }

            return ms.ToArray();

        }

This is relatively resource intensive of course, and will use some of your sandboxed resources.  It hasn’t been a problem for me, but your mileage may vary.

Connecting to WCF Service from XNA on Windows Phone 7

If you are using XNA Game Studio 4.0 to create an application targeted to Windows Phone 7, you may have tried to add a service reference to a WCF service inside visual studio and been stuck…

 

This appears to be a known issue – there is an easy workaround doing this manually with the Silverlight version of the SvcUtil application.

 

  1. Launch a command prompt
  2. Navigate to: C:Program Files (x86)Microsoft SDKsWindows Phonev7.0Tools
  3. Run SLSvcUtil /d:<output directory> <path to wsdl>
  4. Add the two created files (*.cs, and ServiceReferences.ClientConfig) to your existing XNA Project
  5. Select ServiceReferences.ClientConfig if the Solution explorer and change “Build Action” to “Content”

 

Here is an example of using the command:

C:Program Files (x86)Microsoft SDKsWindows Phonev7.0Tools>slsvcutil /d:c:mycode  http://test.com/test.svc?wsdl

 

Be sure to leverage the MSDN article for more information:

http://msdn.microsoft.com/en-us/library/cc197958%28v=vs.95%29.aspx

Remove Blocked File Types in SharePoint

You can remove the blocked file types in SharePoint programmatically by accessing a collection on an SPWebApplication object.

Collection<string> be = webApplication.BlockedFileExtensions;
if (be.Contains("dll")) be.Remove("dll");
webApplication.Update();

Add Web Part to Page Programmatically

I use the following code to programmatically add web parts to my pages.

I frequently see similar code, but no one is ever using the xml from the web part library to do the import, and as such the properties are not set to what was exported/defined in the web part xml.

Specifically:

XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());

WebPart webPart = manager.ImportWebPart(xmlReader, out errorMessage);

 

 

Enjoy:

 

public static string AddWebPartToPage( SPWeb web, string pageUrl, string webPartName, string zoneID, int zoneIndex) { using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager( pageUrl, PersonalizationScope.Shared)) { using (System.Web.UI.WebControls.WebParts.WebPart webPart =

CreateWebPart(web, webPartName, manager)) { manager.AddWebPart(webPart, zoneID, zoneIndex); return webPart.ID; } } } public static WebPart CreateWebPart(SPWeb web, string webPartName, SPLimitedWebPartManager manager) { SPQuery query = new SPQuery(); query.Query = String.Format(CultureInfo.CurrentCulture, "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>{0}</Value></Eq></Where>", webPartName); SPList webPartGallery = null; if (null == web.ParentWeb) { webPartGallery = web.GetCatalog( SPListTemplateType.WebPartCatalog); } else { webPartGallery = web.Site.RootWeb.GetCatalog( SPListTemplateType.WebPartCatalog); } SPListItemCollection webParts = webPartGallery.GetItems(query); XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream()); string errorMessage; WebPart webPart = manager.ImportWebPart(xmlReader, out errorMessage); return webPart; }

SharePoint Raw Text Field Control

Sometimes you simply need to dump out the contents of a text field raw, and not have the SharePoint field controls alter you HTML, strip scripts, etc. 

I’m currently using this to display raw HTML that is being programmatically imported into a list.

This is a very simple example of how to make a field control, however you’ll want to ensure you consider things like caching and security before deploying this.

 

    public class RawTextFieldControl : BaseFieldControl
    {
        public XnaRawTextFieldControl()
        {
            base.DisableInputFieldLabel = true;
            base.ControlMode = SPControlMode.Display;
        }

        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {
            output.Write(this.ItemFieldValue);
        }
    }

SharePoint Authenticated User Panel

When using SharePoint 2007 (MOSS) or SharePoint 2010 as a web content management system, it’s common to have pages show different content to logged in users and anonymous users.

Sometimes you can achieve what you want via audiences or security trimming, but often I find it’s easier to use an overloaded panel that you can include in your page layouts or master pages.  You can then set public properties to determine who the content renders for (anonymous users, specific roles, etc).

 

public class AuthenticatedPanel : Panel { public bool ShowForAnonymous { get; set; } public bool ShowForAuthenticated { get; set; } public string ShowForRole { get; set; } protected override void Render(HtmlTextWriter writer) { if (ShowForAnonymous) { base.Render(writer); return; } if (ShowForAuthenticated &&

HttpContext.Current.User.Identity.IsAuthenticated) { base.Render(writer); return; } if (!String.IsNullOrEmpty(ShowForRole) &&

HttpContext.Current.User.IsInRole(ShowForRole)) { base.Render(writer); return; } } }