Skip Ribbon Commands
Skip to main content
SharePoint

Excellence In Technology

 This Blog Site Information

Hello everyone.  After blogging quite a bit and taking a nearly 3 year break, I am back.  I'm working on getting this site back up to par by reposting all of my older blogs.  After that new blogs and comments and feedback.
Mar 23
String.Format() vs. String Concatenation

So the other day I was doing a code review for a fellow work colleague and I noticed more and more people on my team are using String.Format("{0} {1} {2}) vs. string + string + string when concatenating strings. Our coding standards don't address this particular issue. I have read up on the differences and pros and cons many times. This time, I decided to undertake my own tests.

I created a simple application for my purposes and then looked at the IL (Intermediate Language) through ILDASM to see what's going on under the hood. This article is not meant to discuss all possibilities such as using the Sytem.Text.StringBuilder.

String Interning

Before you read any further, you must understand a little about the CLR and string manipulation. To be as brief as possible, every time your code creates a string, the CLR may add an entry to what's called the intern pool. It's called String Interning.

The common language runtime (CLR) conserves string storage by maintaining a table, called the intern pool, which contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

What this means is if you have code like this:

string one = "Shane";

string two = "Shane";

 

There will only be a single instance or occurrence of "Shane" in the intern pool.

Format vs. +

Back the subject matter.

Let's compare the two lines of code that concatenate strings. They both concatenate a string.

// Declare vars

string empty1 = "";

string empty2 = string.Empty;

string something1 = "Shane Weebe";

string something2 = "Landon Donovan";

 

// Concact strings

string concact1 = "something2: " + something2 + ", something1: " + something1 + ", empty2:" + empty2 + "empty1: " + empty1;

string concact2 = string.Format("empty1: {0}, empty2: {1}, something1 {2}, something2 {3}", empty1, empty2, something1, something2);

 

Both lines of code accomplish the same task at the end of the day: concat1 and concat2 will contain the same string. They both concatenate a string. Looking at the IL (intermediate language), which is what the .NET compiler creates, here is what we see:

Concact1 (I removed some lines for easier viewing). The orange lines are constructed from the hard coded strings in the c# code; the blue lines are constructed from the variables.

IL_001a: newarr [mscorlib]System.String

IL_0021: ldloc.s CS$0$0000

IL_0023: ldc.i4.0

IL_0024: ldstr "something2: "

IL_002a: ldloc.s CS$0$0000

IL_002c: ldc.i4.1

IL_002f: ldloc.s CS$0$0000

IL_0031: ldc.i4.2

IL_0032: ldstr ", something1: "

IL_0038: ldloc.s CS$0$0000

IL_003a: ldc.i4.3

IL_003d: ldloc.s CS$0$0000

IL_003f: ldc.i4.4

IL_0040: ldstr ", empty2:"

IL_0046: ldloc.s CS$0$0000

IL_0048: ldc.i4.5

IL_004b: ldloc.s CS$0$0000

IL_004d: ldc.i4.6

IL_004e: ldstr "empty1: "

IL_0054: ldloc.s CS$0$0000

IL_0056: ldc.i4.7

IL_0059: ldloc.s CS$0$0000

IL_005b: call string [mscorlib]System.String::Concat(string[])

IL_0060: stloc.s concact1

IL_0062: ldstr "empty1: {0}, empty2: {1}, something1 {2}, something2 {3}"

Concact2 (I removed some lines for easier reading). Notice only blue lines from the variables.

IL_0068: newarr [mscorlib]System.Object

IL_006d: stloc.s CS$0$0001

IL_006f: ldloc.s CS$0$0001

IL_0071: ldc.i4.0

IL_0074: ldloc.s CS$0$0001

IL_0076: ldc.i4.1

IL_0079: ldloc.s CS$0$0001

IL_007b: ldc.i4.2

IL_007e: ldloc.s CS$0$0001

IL_0080: ldc.i4.3

IL_0083: ldloc.s CS$0$0001

IL_0085: call string [mscorlib]System.String::Format(string, object[])

IL_008a: stloc.s concact2

If you look at concatenating using the + operator (concat1), the compiler creates a new string array, fills the array with the various variables and hard coded text, then calls String.Concat to combine them, nearly replicating the c# code from concat2.

If you look at the String.Format method (concat2), the compiler creates a new object array, fills the array with the various strings and then calls String.Format method to process the concatenation.

Summary

Looking at the IL the String.Format method appears to be a more optimized approach to string concatenation as the IL doesn't have to combine the hardcoded text and variables in a String array, nor does it have to call String.Concat.

However, let's not forget about the intern pool mentioned above. While the compiler creates the IL, the CLR will still use the intern pool to manage string resources in memory. Since we had declared the strings with default values, those values will be in the intern pool in both concat1 and concat2. This means, that even though the IL created a string array for the Concat1 and, consumed a few more lines of IL code, in terms of memory there is no additional overheard.

My conclusion is use what you feel is more readable. I think String.Format is smarter when you are combining many strings, but the + operator is easier and more readable for simple concatenations.

May 10
Enabling Anonymous access in a SharePoint site

You would think it would be a simple process to enable anonymous access in MOSS 2007 or WSS 3.0. If you are IIS experienced, you might be tempted to just use your knowledge of IIS to turn off/on anonymous access and turn on/off NT authentication. While this was the way of things with WSS 2.0 and SPS 2003, it is not the Jedi way with MOSS 2007 or WSS 3.0.

Why enable anonymous access in the first place? To allow un-authenticated users visibility to your SharePoint web application and one or more site collections. You will still have trouble with Office Documents and authentication prompts, but that is a story for another day.

The problem with attempting to enable anonymous access in SharePoint is that it you can't always find the area where anonymous access is enabled. Here are the steps:

  • Enable anonymous access at the web application level
  • Enable anonymous access at the site collection level.

 

  1. Load Central Administration; navigate to Application Management->Authentication Providers. In my case, I want internal folks inside my firewall to use NT Authentication, while outside I want anonymous access. To make this possible I first had to extend my web application, which is a separate topic, however you don't need an extended web application to enable anonymous access. I am going to enable anonymous access on my Internet Zone. You may just have a Default zone which is fine. Whatever the case, click the link.
  2. On this screen you want to click the Enable anonymous access checkbox then click Save.
  3. This is only the first step. You now need to enable anonymous access at the site collection level. You must have site collection administration access to make this happen. This is where things can get weird… I'll explain as I go.

  4. Load your site collection in your browser, and at your site collection root go to Site Actions->Site Settings, Navigate to Advanced Permissions.
  5. Click on Settings->Anonymous Access.
  6. Here is that strange part I mentioned. You may not see Anonymous Access as a menu selection. It's gone, you don't see it, and it is not visible. This will only happen if you have not enabled anonymous access for the Authentication Provider that this site collection resides under at the Web Application level (see beginning of this blog). This can be confusing if you have more than one Authentication Provider. For example, if I log into http://www.TheEIT.com, I will see this option, but if log into http://eit, I will not. That is because http://www.TheEIT.com is my internet authentication provider with anonymous access enabled, while http://eit is the default authentication provider with anonymous access turned off. They both point to the same content database and both render the same SharePoint content, but the network route to get there goes through different IIS Web Applications. You can prove this out by directly accessing the anonymous access page. It is located under _layouts/setanon.aspx.
    1. If you access _layouts/setanon.aspx on a site whose web application does not have anonymous access enabled, the options on this page will appear as Read Only, otherwise they will be editable.
    2. From this screen, you can enable Anonymous access.
May 04
Change the Title of a MOSS/WSS SharePoint Page

So today I created a Basic Page in SharePoint. These pages are simple pages that allow you to edit the content without having to drop a content editor web part. When you create the page, it must be stored in a document library.

 

After creating the basic page and pumping in my content, I clicked Create to save and everything looked good except in the top of my browser it said "Basic Page". Nowhere is there a place to put in the page title.

So, just Edit Properties of the page? Nope, doesn't have a Title to edit. So what do you do? You have to crack open SPD (SharePoint Designer).

  1. Open the site in SPD.
  2. Navigate to the Basic Page and double click to edit it.

  1. Find this section:

    <asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">

    <SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,webpagecreation_bp_title%>" EncodeMethod='HtmlEncode'/>

    </asp:Content>

  2. Change the <SharePoint:EncodedLiteral… section to the following.

    <asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">

       Shane is Cool Page

    </asp:Content>

  3. Save the page and refresh it in your browser. It now looks much better, doesn't it?

Apr 07
Reclaiming 5 minutes of your life

You are a good employee because you want to store your documents in SharePoint instead of sending them in emails or storing them locally on your desktop. Be proud, you have made the correct choice!

But you have noticed that there are painful extra steps

…You have just finished working on a document in Word, and you want to save into SharePoint…you first save the document to your desktop, then open up Internet Explorer, then navigate to your SharePoint site, you get prompted to authenticate, then find the document library sub site… you take a quick break and get a coffee… back at your desk, you click Upload, you navigate to your My Documents folder, sort through a few sub folders and finally upload the file…. SharePoint has your file, and you have now taken an extra 5 precious minutes from your workday!

I am going to help you reclaim those 5 minutes…

This works with both Word 2003 and Word 2007. What we are going to do is add the location of your document library to your Save As section of your Office applications. You are only required to do this once. Once you have performed these steps you will be able to save directly into your SharePoint site. (note that if your site currently resides on a URL with CENTER.SAIC.COM, you will still have to pre authenticate to a center site for this to work, if your site currently resides on a URL with ICENTER.SAIC.COM, then you will not have to pre-authenticate).

 

With word 2007

 

  1. Click the Office button and select Publish or Save As.
  2. Select Document Management Server or Word Document.
  3. The Save As Screen appears. Once you are here the procedure is the same for Word 2003 and Word 2007. This screen is nearly the same as selecting Save As and selecting My Network Places, except it's filtered

With word 2003

 

  1. Click the File-Save As menu.
  2. The Save As Screen appears. Once you are here the procedure is the same for Word 2003 and Word 2007

 

Save As screen

  1. While at this screen, click the My Network Places option in the left panel. You may already see the SharePoint document library you wish to save into.
  2. If you do not see the SharePoint library, then select the Tools button in the lower left and select Map Network Drive. The Map Network Drive screen appears.
  3. Click the check box Reconnect at logon.
  4. Click the link at the bottom: Sign up for online storage or connect to a network server.
  5. The Welcome to the Add Network Place Wizard appears. Click Next
  6. At the Add Network Place Wizard, select Choose another network location.
  7. Type the URL of your SharePoint site and document library. For example, https://center.saic.com/help/Help Documents. Click Next.
  8. You will be prompted to Authenticate. Be sure and Authenticate with your SAIC-EXT\username account and click OK.
  9. At the What do you want to name this place screen, type a meaningful name. The name should start with the name of your document library followed by the name of the site
  10. Click Next.
  11. At the final screen, unselect Open This network place when I click Finish. (note if you leave this selected, a window will open and appear to spin forever… behind the window, you are being asked to authenticate again).
  12. Click Finish.

Ok, so now what have you accomplished? Good question - you have just saved your document library URL into your local machine's registry. From this point forward, you can open an Office document, and click Save As to save directly into your SharePoint document library. Let's try it out

 

Never waste 5 minutes again

From this point forward, follow these simple few steps.

  1. Open an office document on your desktop.
  2. Click Save As and select My Network Places on the left panel.
  3. Navigate to the network place you just stored from the above steps
  4. Give the document a name and click Open. You may be prompted to authenticate, but then you should get to a screen that looks similar to the following:
  5. Click Save and you are good to go.

 

 ‭(Hidden)‬ Blog Tools

About this blog
Hi, I'm Shane Weebe and am a SharePoint person.  I have been using, consulting, developing, designing, architecting, leading teams, and pretty much doing anything that can be done with SharePoint since it was introduced in 2001!  You can catch my classes at UCSD or find me on LinkedIn to connect.