Follow Us:
The more I learn and get to know PowerShell, the more I love it. I am able to perform what used to be time-consuming tasks (many hours or days) in seconds (after you write the script of course). While writing a script recently to perform some tasks on numerous SharePoint sites (webs to be precise), I was constructing URLs for the sites and using as variables and I needed only part of the URL (the managed path). After a little poking around, I found a nice easy way to do it.
In SharePoint (among other things), URLs can be of two different types, absolute and relative. The absolute URL for an object is the full path direct to that object, where the relative URL is an abbreviated path that combines the base URL and the current location. Let’s look a couple examples:
Absolute URL
Relative URL
The relative URL removes the base URL http://portal, which is the root web of the web application. This becomes important especially if you use SSL on a web application, where you allow access via SSL and non-SSL, or if you use Alternate Access Mappings. If you use absolute URLs, the other website will never get used. This is why it is crucial to use relative paths to URLs in your navigation links like a good SharePoint administrator. But I digress… on to PowerShell!
Without checking, I first solved this problem the complicated way. I would enumerate the full URL, and then only grab the text 5 or however many characters I wanted to remove. You can do this by using $webURL.Substring(3), where 3 is the number of characters you want to remove. If you do this, be sure to do this on the URL as a string, NOT a web object as Substring is not a method on the web object, but is available on strings. You could have also have used the TrimStart method as well I suppose.
This worked, but there had to be a better way! In PowerShell, I think one of the most powerful commands to get to know is Get-Member. Using this, I discovered a property called ServerRelativeURL on both a site object (site collection) or web object. To see this, you pass (pipe) the site or web object to the get-member command like this:
1: get-spweb http://contoso.intranet.com/it/sandbox | get-member
This returns all methods and properties of that object (our subsite in this case). There are two properties that are related, Url and ServerRelativeURL. So let’s look at this:
1: $web = get-spweb http://contoso.intranet.com/it/sandbox
2: $web.ServerRelativeURL
The result is:
/it/sandbox
Voila! Exactly what I needed. We can then store this in a variable, and use it wherever we need it. As you might expect, Url returns the absolute URL of the object. I strongly encourage you to investigate get-member, It opens up some many possibilities to view and/or various properties of almost anything. Hope it helps!
Dtarver,Thanks for the reply, but I'm not quite sure I understand what you're asking. Can you provide more information regarding how you are using the images URLs? Typically images for the site would be stored in either the /SiteAssets library, or a dedicated image or assets library you create. Those images are then referenced in the site via navigation links, web parts, etc. and that is where their relative paths are used. For example, you insert an image web part on a page, and you would specify the relative URL for the image in the web part properties (/it/images/sitelogo.png). How are you moving these images from development to production? Are you using the OOTB methods of backup or export/import powershell, or are you using a tool? If for example you're migrating a configured site collection, you could use site collection backups. When the site collection backup from DEV is restored to production, the restore will pick up the URL from production, and any relative paths in the site collection will automatically be updated. There are only a couple things that don't support relative paths in the UI, like the Portal Site Connection. The UI requires http://, but if you set it with PowerShell, it be relative as well.If I'm missing something or you still have questions, feel free to drop me a line at doug.allen@c5insight.com.
The information in your post about SharePoint is more interesting. and this info is more useful for the developers to develop the SharePoint apps. Thanks for share this valuable info.
HiWhat method do you use to remove the absolute path for images that are stored in an image library when migrating from 'staging' to 'production' environments?I noticed that when I add images into a "list" SharePoint 2010 placed the absolute path to the image even though I typed the relative path. This will be a problem when we migrate from staging into production since the staging domain is a different name than the production environment.What is the best wrok around for migrating and minimizing re-work?Thank you!
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.