Showing posts with label 2010. Show all posts
Showing posts with label 2010. Show all posts

Saturday, October 19, 2013

Enable Publishing Feature recursively in SP 2010 using PowerShell

So now that we saw an inconsistency among multiple sites, with some having Publishing feature enabled while others not, we decided to run a script which combs through each site within a site-collection and enables this feature, if not already enabled.

Given the situation that we had approx. 25-50 sites/sub-sites in each of the 7 site-collections, this appeared fair-enough to tackle one site-collection at-a-time.

To meet this requirement, we had to perform the below 2 high-level tasks:

1. Check and enable the [SharePoint Server Publishing Infrastructure] at the root site-collection level, if not already enabled. (Can be seen from “Site collection features” on “/_layouts/settings.aspx” page).

image

2. Check and enable the [SharePoint Server Publishing] at the site-level, if not already enabled. (Can be seen from “Manage site features” on “/_layouts/settings.aspx” page).

image

Enabling these features, creates the below 4 Lists & Libraries.

image

image

Now the script:

Step 1 was achieved by the script below:

# The URL to the Site Collection
$spSite = Get-SPSite “http://siteurl/”
Write-Host "Root Site Collection: " $spSite

$PublishingSitefeature = Get-SPFeature PublishingSite
$siteCollection = Get-SPSite $siteUrl

#Check and Enable the feature on the site collection
if((Get-SPFeature $PublishingSitefeature -Site $siteCollection.Url -ErrorAction SilentlyContinue) -eq $null )
   {
       write-host -foregroundcolor Yellow "Activating the PublishingWeb feature on " $siteCollection.Url
       Enable-SPFeature $PublishingSitefeature -Url $siteCollection.Url   
       write-host -foregroundcolor Green "Activated the PublishingWeb feature on " $siteCollection.Url
   }
   else
   {
      write-host -foregroundcolor red "PublishingWeb feature already activated on : " $siteCollection.Url
   }

Step 2 was achieved by the following script:

$siteCollection | Get-SPWeb -limit all | ForEach-Object{
   if((Get-SPFeature "PublishingWeb" -Web $_.Url -ErrorAction SilentlyContinue) -eq $null )
   {
      write-host -foregroundcolor Yellow "Activating the PublishingWeb feature on " $_.Url
      Enable-SPFeature -Identity "PublishingWeb" -Url $_.Url
      write-host -foregroundcolor Green "Activated the PublishingWeb feature on " $_.Url
   }
   else
   {
      write-host -foregroundcolor red "PublishingWeb feature already activated on : " $_.Url
   }
}

But as every project is seen over-running the estimates. here too we did not have luck to our side.

After Step 1 was performed successfully, we hit the first issue when started the Step 2.

But in my case, I got the below error, and could see that only [Pages] library got created (just few mins ago) and [Images] library pre-existed ( not sure who created that 6months ago).

“Enable-SPFeature : Provisioning did not succeed. Details: Failed to create the 'Images' library. OriginalException: The feature failed to activate because a list at 'PublishingImages' already exists in this site. Delete or rename the list and try activating the feature again. At C:\Users\abcd\Documents\PS\ActivatePublishingFeatureonAllSites.ps1:86 char:23
+ Enable-SPFeature <<<< -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature + CategoryInfo : InvalidData: (Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature”

So I deleted this [Images] library using SPD 2010.

image

image

Deleting [Pages] required me to delete the [Forms] sub-folder and then all the sub-folders and files as below within the [Forms] sub-folder:

image

The above files were deleted using SPD 2010, but couldn’t delete the [Forms] folder. So I renamed the [Pages] library to [Pages2] and tried to re-run the PS script again. But this did not solve the issue. This time I received the below error:

“Enable-SPFeature : Provisioning did not succeed. Details: Failed to create the 'Pages' library. OriginalException: There can only be one instance of this list type in a web. An instance already exists. At C:\Users\abcd\Documents\PS\ActivatePublishingFeatureonAllSites.ps1:86 char:23
+ Enable-SPFeature <<<< -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature + CategoryInfo : InvalidData: Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature”.

The reason for this being that you cannot delete few libraries like [Pages] in a site which have the publishing feature enabled. For the case of libraries like the [Images], [Documents] or even the [Site Collection Documents], it is not possible to remove them from the lists settings page or SPD or the manage content and structure tool.

It appears that these lists and libraries have a property AllowDeletion, which has been set as False.

We can use the below script to verify this:

$web = Get-SPWeb “http://siteurl/abcd/”
$lib = $web.Lists[“Pages2”]
$lib.AllowDeletion

This returns “False” as seen below:

image

So what do we do?

PowerShell to rescue again. This allows you to set the property to “True” and then successfully delete the library.

Use the below script to set the property to “True”:

$web = Get-SPWeb “http://siteurl/abcd/”
$lib = $web.Lists[“Pages2”]
$lib.AllowDeletion = $True
$list.Update()

I could then easily use SPD or the browser to delete the [Pages] library (in my case I renamed it to [Pages2]).

Finally I re-executed my script successfully and got the feature enabled.

Friday, October 18, 2013

Excel Services error - This workbook is larger than the maximum workbook size allowed to be opened in the browser

In scenarios, where you need to develop quite large Excel PowerPivot files and deploy to SharePoint, there is are two interesting steps to be performed.

Step 1: You need to enable the ReporServer to allow doing so by adding a parameter

“maxRequestLength=100000” to the “httpRuntime” in the web.config file for Reporting Services.

This web.config file usually resides in a location similar to “C:\Program Files\Microsoft SQL Server\MSRS10_50.SQLEXPRESS\Reporting Services\ReportServer”. (Your drive letter or the path may differ based upon your install).

Once set, do an IISREST.

When this parameter is not set in the web.config, you might see an error like:

“There was an exception running the extensions specified in the config file. ---> Maximum request length exceeded.”

Step 2: In addition to Step 1, you also need to raise the default file size limit on Excel Services on SharePoint, else you get an error:

image

Cause: OOB, the maximum workbook upload size allowed in SharePoint 2010 is 10mb.

To get rid of the above error, perform the below steps:

a. Go to Central Administration => Application Management =>Manage service applications.

b. Click Excel Services Application (click the parent, not the child proxy).

c. Click Trusted File Locations.

d. Click the location. This will be an added trusted file location for the site collection or a new file location, usually like “https://”.

e. Scroll to Workbook Properties.

f. In Maximum Workbook Size, increase the file size from 10 to 50.

image

Click OK. Refresh your report page to see it load without any errors.

Saturday, August 31, 2013

A generic web part to display any List or Library from any site

I have seen this question asked so many times over and over again, that it makes me think why Microsoft doesn’t provide this web part out-of-the-box. My client needed an easy way to just display few List columns on a child-site (while all other columns remain hidden), from a List available on the parent site. That should have been easy, pretty much, having the proper security in-place. But wait !! Much to my disappointments, I was stuck with every available options/solutions that I found on the www, after progressing to some extent.

Well, I know there are few that are available to choose from, but with hell lot of limitations:

-You can use the OOB Content Query web-part (CQWP). This appears to be a “hot” favorite for most people out there, but seems like you have to mess around with the xslt files to add more columns and get the things working. Yikes! my client didn’t want to be doing that every time you need to change the display view.

-You may use these code-plex solutions (this & this ) that does say about extending this CQWP and getting the job done. But I just couldn’t get that working. My bad! may be you are lucky.

-You can use the third-party Lightning Conductor web part, which was not acceptable to my client either.

So here I am. Came-up with this idea of setting up a custom web-part with custom properties to cater to my client needs. This seems pretty much generic in nature in my environment, given the site-hierarchies and security/permissions in-effect. This may not be exact same in your case and would need some re-thinking in different scenarios.

My Scenario:

-Parent site accessible only to the Finance department (who are AD users).

-Parent Site contains a List “Timecards” containing 12 different columns.

-One of its Child site “Contractors” have the security inheritance broken, and is “read-only” accessible to all AD-Users.

Client Requirement:

-All users of Finance department can see and access all 12 columns of this List “Timecards” (which is in-fact, true by default).

-All users of all departments can see “ONLY” few columns of this List “Timecards” on the child site “Contractors” which is read-only access to all.

-Site owners should be able to easily-control the specific columns to be made available for viewing on the child site.

Solution:

So what I proposed was a custom web part, that pulls the columns made available to a specific “VIEW” of the List “Timecards” containing only the needed columns on the parent site. The view can be easily maintained and modified by the site owners. In essence, this web part can actually connect to any site and pull any list data for any specified view. The data would be displayed using the SPGridView with CSS classes attached to each column for further look-n-feel changes.

Input parameters:

[SiteURL] : A custom property to specify the url to the source site hosting the List.

[ListName] : A custom property to specify the name of the source List.

[ViewName] : A custom property to specify the name of the View made available in the source List.

Implementation:

Step 1: Define Variables and properties

// Set the default values
        private string c_SiteURLDefault = "";
        private string i_ListView = "AllItems";
        private string i_ListName = "Shared%20Documents";
        private SPGridView _gridView;
        private string ROW_CSSCLASS = "nch-row-style";

// Create custom category in the property sheet and the properties as well
        [Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DisplayName("Site URL")]
        [WebDescription("Site URL containing the List.")]
        public string SiteURL
        {
            get { return c_SiteURLDefault; }
            set { c_SiteURLDefault = value; }
        }

        [Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DisplayName("List Name")]
        [WebDescription("Name of the List.")]
        public string ListName
        {
            get { return i_ListName; }
            set { i_ListName = value; }
        }

        [Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DisplayName("List Name")]
        [WebDescription("View Name of the List to display.")]
        public string ListView
        {
            get { return i_ListView; }
            set { i_ListView = value; }
        }

Step 2: Initialize Constructor

       public DisplayListsLibrariesWebPart()
           : base()
       {
           _gridView = null;
       }

Step 3: Define other methods

//Method to return a data table populated with all items from a list passed to it.

       private DataTable GetSource(SPList splist)
       {
           DataTable table = splist.Items.GetDataTable();

           return table;
       }

//Method to check if a List contains a specific View. Returns “True” if found else returns “False”.

       private bool HasView(SPList list, string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
                return false;

            foreach (SPView view in list.Views)
                if (view.Title.ToLowerInvariant() == viewName.ToLowerInvariant())
                    return true;

            return false;
        }

Step 4: Define CreateChildControls

            LiteralControl errmsg = new LiteralControl();
            //Variable to store a list of accessible sites.
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            //Define your CSS styles. These classes can be over-written from within CEWP codes on the page where this web-part would be deployed.
            sb.Append(@"<style type='text/css'>");
            sb.Append(@".ms-vh2-gridview{text-align:left;}");
            sb.Append(@"</style>");

            try
            {
                if (SiteURL.ToString() != "") //if Site URL is provided in the web part property.
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        //Access the Site URL from the web part properties.
                        using (SPSite site = new SPSite(SiteURL.ToString()))
                        {
                            #region Open an instance of the web.

                            //Open an instance of the web.
                            using (SPWeb targetWeb = site.OpenWeb())
                            {
                                // targetWeb provided using an absolute url is now available through OpenWeb() call directly.
                                //Next, retrieve all the user-accessible sites-subsites of the "Site URL" site-collection.

                                //Check if the List exists
                                SPList myList = targetWeb.Lists.TryGetList(ListName);
                                if (myList != null) //If List exists
                                {
                                    #region Check View exists - If-Else.

                                    //List exists, now check if the List View exists.
                                    if (HasView(myList, ListView))
                                    {
                                        //View exists, get the view.
                                        SPView listView = myList.Views[ListView];

                                        //Get the columns of the view
                                        SPViewFieldCollection viewfieldCollection = listView.ViewFields;

                                        DataTable table = GetSource(myList); //Get the List Items in a datatable.

                                        //if data-table returns rows.
                                        if (table != null)
                                        {
                                            _gridView = new SPGridView(); //Instantiate grid
                                            _gridView.ID = "_gridView";
                                            _gridView.AutoGenerateColumns = false;
                                            _gridView.Width = new Unit(100, UnitType.Pixel);
                                            _gridView.DataSource = table.DefaultView;

                                            //Set Row Alternating back color style
                                            _gridView.CssClass = ROW_CSSCLASS;

                                            string sColor = "#eeeeee";
                                            Int32 iColorInt = Convert.ToInt32(sColor.Substring(1), 16);
                                            Color alternatingRowStyleColor = System.Drawing.Color.FromArgb(iColorInt);
                                            _gridView.AlternatingRowStyle.BackColor = alternatingRowStyleColor;
                                            _gridView.EnableViewState = false;


                                            #region Get data from the list view.

                                            // Get data from the list view.
                                            SPListItemCollection items = myList.GetItems(listView);

                                            //Display each column values for each rows
                                            foreach (string viewFieldName in viewfieldCollection)
                                            {
                                                SPField columnDet = myList.Fields.GetFieldByInternalName(viewFieldName);

                                                _gridView.Columns.Add(new SPBoundField { DataField = viewFieldName.ToString(), HeaderText = columnDet.ToString() });

                                            }

                                            _gridView.DataBind();
                                        }
                                        else
                                        {
                                            //Data in the list doesnot exist.
                                            errmsg.Text = "No data found in the Source List.";
                                            throw new Exception(errmsg.Text.ToString());
                                        }

                                        #endregion
                                    }
                                    else
                                    {
                                        //View doesnot exist.
                                        errmsg.Text = "List View parameter invalid or cannot be empty. Please provide the correct value by editing the web part custom properties.";
                                        throw new Exception(errmsg.Text.ToString());
                                    }

                                    #endregion
                                }
                                else
                                {
                                    //Invalid List name. Throw exception.
                                    errmsg.Text = "Site URL or List Name parameter(s) invalid or cannot be empty. Please provide the correct value by editing the web part custom properties.";
                                    throw new Exception(errmsg.Text.ToString());
                                }
                            }

                            #endregion
                        }
                    });
                }
                else
                {
                    errmsg.Text = "Site URL parameter invalid or cannot be empty. Please provide the correct value by editing the web part custom properties.";
                    throw new Exception(errmsg.Text.ToString());
                }
            }
            catch (Exception ex)
            {
                //Display the error message.
                errmsg.Text = ex.Message.ToString();
                this.Controls.Add(errmsg);
                return;
            }

            //Add the styles for the gridview.
            LiteralControl mystyles = new LiteralControl();
            mystyles.Text = sb.ToString();
            this.Controls.Add(mystyles);

            //Display the list gridview.
            this.Controls.Add(_gridView);
            this.Controls.Add(new Literal { Text = "<hr/>" });

Step 5: Compile, Build & Deploy !!! If needed activate the feature, add the web part to the page and set the custom properties. You get a clean sharepoint like UI displaying the list rows.

Thanks for going through this entire long post !

Saturday, July 27, 2013

SharePoint Designer Check-in error

While trying to perform a check-in on an existing checked-out page in the “Pages” library of a publishing site, I fell prey to this error.

“SharePoint Designer cannot perform this operation. The file is no longer checked out.”

Got it fix by deleting the cache files located at here:

“%APPDATA%\Microsoft\Web Server Extensions\Cache”

Saturday, July 6, 2013

Remove the Group Label from the “Group By” List Views

This had to be simple java-script, so a quick search on the internet returned me a 2011 Blog post by “Alexander Bautx” which works “AS-IS”. I had to just replace the single-quotes (‘) by deleting and inserting it back again for all of it’s existence, and that’s it. Probably it was due to the copy-paste formatting issue. I have highlighted the change below, which appears at 4 places in his code.

But this was awesome!! Kudos to Alexander.

Note: You may not need the first line reference for the jquery, if you already have it in your master page.

$(this).find("a:last")[0].nextSibling.nodeValue = '';

Sunday, May 26, 2013

Export Term Store to csv using PowerShell

This is another approach towards exporting the term stores to a CSV file format using power shell. I have tried to modify and enhance my previous post to achieve this requirement of generating a  Microsoft Excel format.

So here it is.

#creating a header-row for the csv file

"termStore `t" + "termGroup `t" + "termGroup-LastModifiedDate `t" + "termSet `t" + "termSet-LastModifiedDate `t" + "termSetGUID `t" + "terms `t" + "termsGUID `t" + "terms-LastModifiedDate" >> C:\ExportTermStore.csv

$termSet = Get-SPTaxonomySession -Site http://mydomain/sites/cthub
$termStore = $termSet.TermStores[0]
$termStore.Groups | FT -Autosize Name
$termGroup = $termStore.Groups["myTermStoreGroupName"]   #This is the case when we know the name of the group and want just that. We can also have a foreach instead, to loop-through each of the TermStore Group. I chose the first.
$termStore = $termStore.Name #saving the termStore “Name” in the variable $termStore
$termGroup.TermSets | FT -Autosize Name

foreach ($termGroups in $termGroup.TermSets)
{
  foreach ($termSets in $termGroups.Terms)
  {
    foreach ($terms in $termSets.Terms)
    {
      $termStore + "`t" + $termGroups.Name + "`t" + $termGroups.LastModifiedDate + "`t" + $termSets.Name + "`t" + $termSets.LastModifiedDate + "`t" + $termSets.ID + "`t" + $terms.Name + "`t" + $terms.ID + "`t" + $terms.LastModifiedDate >> C:\ExportTermStore.csv
    }
  }
}

Saturday, April 20, 2013

Formatting Texts in a SharePoint List Item columns

This could be an old-school  thought of wanting to apply different formats to the values stored in list columns.

Well, one can definitely go a number of ways to achieve that, but here’s an approach when an end-user can drop some code in a calculated field to achieve this very easily and more precisely, getting to a number of different formatting styles.

Format 1: Displaying a Hyperlink Text in a calculated column

Create three columns in a list as below:

Column1: [ActualURL] of type “Single line of text”. (We will store values like “http://neerajch.blogspot.com” in this column)

Column2: [URLTitle] of type “Single line of text”. (We will store values like “NeerajCh” in this column)

Column3: [HyperlinkHTML] of type “Calculated”.

Type the below in the Formula box:

=CONCATENATE("<DIV><a href='",ActualURL,"'>",URLTitle,"</a></DIV>")

Select “Number (1, 1.0, 100)” in the [Data Type returned from this formula] section.

Leave the “Automatic” in [Number of Decimal Places].

That’s it. Hit OK and create one new list entry with values mentioned above. [Title] can be anything.

You should see something like this. Hurray !!!

image

Now that “Concatenated” formula piece can be a real handy and fruitful, if used wisely. Let’s see another small example.

Format 2: Displaying a Hyperlink Text in a calculated column as BOLD

With the same configurations for the list columns as above, we modify the formula to include <b> & </b> tags within the <DIV>.

=CONCATENATE("<DIV><b><a href='",ActualURL,"'>",URLTitle,"</a></b></DIV>")

And now you see something like this:

image

Wow, I know, you must be smiling. Smile Nothing great here!! Just plain HTML. And that is my intent.

Let the things be as simple as they can be, for this has to be done by an end user not a developer/designer.

So the end users now have a great handy tool and can make it even powerful, if they can extract more complex HTMLs from their peer developers and embed here.

Saturday, March 23, 2013

Export Term Store to xml using PowerShell

This seemed so obvious while using Central Admin, but soon I realized that there is so much left for SharePoint team at Microsoft to accomplish.

I needed to discuss the Site Taxonomy with my colleagues during our weekly call, when suddenly I found myself searching for the script that would do the magic.

TechNet has this:

http://social.technet.microsoft.com/wiki/contents/articles/17874.sharepoint-2010-how-to-manage-the-term-store-via-powershell.aspx

Gary LaPointe has this:

http://blog.falchionconsulting.com/index.php/2012/03/exporting-and-importing-sharepoint-2010-terms/

Thanks to the above. This was sufficient for me to quickly get my script in-place:

Export-SPTerms  -Group (Get-SPTaxonomySession -Site "http://mydomain/sites/cthub").TermStores[0].Groups[2] -OutputFile "D:\CTHubExport\site_terms.xml" –Verbose

You probably will need to work with the parameters for TermStores[0] & Groups[2] to see that you are correctly referring to your Term Store and the needed Group within the TermStore.

This generated a beautifully formatted xml file.

Saturday, February 23, 2013

Change position of the “Add new announcement” link

This wasn’t a surprise when it was thrown at me with “Please do not spend much time researching, if you have not already done it in past.”.

The question from the stake-holders was “Does anyone know how to move the ‘add new item’ link at the bottom of lists to the top of the list? (it becomes hard to see and requires scrolling down when many items are added to a list).”

Since I had done that very recently, I came up with a quick solution for her as below:

image

The above highlighted link was moved to top as shown below:

image

This was easily achieved by placing some JQuery code within a hidden content editor on the page this was needed. Although this would move every “Add New…” links on that page, but my stake-holder had no issue with that. She wanted just that. Bingo !!!

Here’s is what you need to use:

<script language="javascript" type="text/javascript">
      $(document).ready(function () {
             $("td.ms-addnew").parent().parent().parent().each(function () {
             $(this).insertBefore($(this).prev());
      });
});

</script>

Saturday, January 19, 2013

Display “Upload Document” button on any web part page

So my client got a little too fancy about having a way to upload documents from anywhere on “a site”, to a particular document library available within that site.

Referring to the SharePoint way of uploading documents as below, I was made to run for a solution that would allow them to add this functionality to any web part page.

image

This wasn’t that bad of a thought. Well, at the end it got out to be quite straight forward, if you are happy and comfortable with java-script.

This is what was done, to allow them quickly add a script in a content editor and display a huge button like this:

image

Now the code:

<div class="ms-uploadbtnlink">
  <button onclick="javascript:OpenNewFormUrl(&#39;Documents/Forms/upload.aspx&#39;);return false;" type="submit">
    <nobr>
     <img alt="Upload Document" src="/_layouts/Images/uploaddoc.png"/>&#160;<span>Upload Document</span>
    </nobr>
  </button>
</div>

Thanks for reading!

Sunday, November 25, 2012

Displaying custom User Name in the Welcome Menu

I have seen so much content about approaching this problem in several ways, which to me are kind of solutions that they either:

-Force you to spend un-necessary long man-hours,

-Write too much of custom codes (writing user controls, etc.),

-Makes you try to understand the Welcome.ascx user control (really?? why? Microsoft has already done that).

So eventually, I ended up writing this post for the benefit of all.

SharePoint is an awesome “PLATFORM”, yes, I call it a platform. Not a Tool, or Application or a CRM or a portal, or a …. and the list goes on. By calling it a platform, I am encapsulating all of them together.

So, with so many stuff already provided out-of-box, we only need to know how to leverage them in a best possible manner to our benefits.

By leveraging, I also mean utilizing the excellent work that is being done by one of the Microsoft’s biggest community, the SharePoint community. You’ll find tons & tons.

The one such work that I am referring to is the SharePoint Services Library on CodePlex. Please find it here.

By using this, I have gotten to get this working in the most minimal way that I could find. Below are the pieces of the code that you would need to get your Welcome menu read aloud “Welcome Tango Charlie”. Pre-requisites being that you have a properly configured User Profile (and of course, if your name is Tango Charlie).

Okay, so we are going to modify our SP 2010 Master Page and place some lines of code in the <head> section.

Step 1: You may download the .js file and insert a script reference to the SPServices library within your <head>…</head> tags:

<script type="text/javascript" src="/_layouts/scripts/jquery-1.4.2.js"></script>
<script type="text/javascript" src="/_layouts/scripts/jquery.SPServices-0.7.2.min.js"></script>

Pre-requisites:

a) You should make sure that you have a JQuery script references before referring to SPServices.

b) Refer the SPServices on CodePlex as the JQuery version requirements are strict for what version of SPServices you are using.

Step 2: Place the below script anywhere within your <head>…</head> tags as long as they follow the tags mentioned in Step 1.

<!-- Format the Welcome Menu -->
<script language="javascript" type="text/javascript">
        $(function() {
            var userFirstName = $().SPServices.SPGetCurrentUser({
                fieldName: "FirstName",
                debug: true
            });

            var userLastName = $().SPServices.SPGetCurrentUser({
                fieldName: "LastName",
                debug: true
            });
           
            var displaySection = $('span.ms-welcomeMenu').children('a.ms-menu-a').children('span');
            var textToShow = 'Welcome ' + userFirstName + ' ' + userLastName;
            // Set the text back to the Welcome Menu
            displaySection.text(textToShow);

          });

</script>

And that’s it. You have your name displayed with the “Welcome” prefix.

Try playing with the combinations/permutations of [fieldName: "FirstName"] and [fieldName: "LastName"] to get different text to display. Good Luck.

Friday, November 23, 2012

No item exists at http://servername/sitename/pagename.aspx?ID=88. It may have been deleted or renamed by another user

So, We have upgraded our MOSS 2007 to SharePoint 2010. One of the other challenges that we have been facing after this Database Attach process is that the links having query string parameters have started to fail (Surprisingly…..this error appears randomly on different links). The error it generates is : “No item exists at http://servername/sitename/pagename.aspx?ID=88. It may have been deleted or renamed by another user”

Well, as soon as I searched for this error, I got to see this Microsoft Support article having a resolution to this issue. You can find it here: http://support.microsoft.com/kb/972225

According to Microsoft, this is a know issue and this is what they suggest:

Change the query string parameter from ID to CustomID. Then change the details page from QueryString(ID) to QueryString(CustomID). Once this is done the page will display with no errors.”

So, when I changed my link URLs to “http://servername/sitename/pagename.aspx?BID=88” everything worked fine.

But here are a couple of questions that has kept me wondering, and I am still looking for an answer to them:

#1. My 2007 sites work perfectly with this piece of code. So does that mean, this issue has been introduced in SP 2010?

#2. Even in 2010, not all the links with query string parameters (?ID=nn) are failing. Some seem to work. This seems very weird. Any answers to this?

Saturday, October 13, 2012

Creating a Countdown Feature based on List definition and Timer Job

In this post, I’ll be elaborating on some of the interesting aspects of SharePoint 2010 programming while trying to work on a project to create a countdown timer job for a visual web part packaged as a SharePoint feature. Some of these would include:

Part 1 - How to include the GUID generator in Visual Studio 2010. (This post)

Part 2 - How to create a Visual web part utilizing custom List Definition and List Instance without using Content Types.

Part 3 - How to include sample data with the List instance deployment.

Part 4 - How to create, deploy and utilize a Timer Job.

Before we jump into the List Definition, let’s first do a prep-work, much needed for the most of the Content development in SP 2010. Even though the GUID Generator can be accessed online here, I prefer to use it in my VS 2010 IDE.

- Start VS 2010. Go to [Tools] –> [External Tools].

- Provide the “Title” = “Create GUID”

- In the “Command”, click the “…” button next to it, and browse to the location “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\guidgen.exe”.

- Click [Add] and then click [OK].

image

You should be now able to see an option to create GUID right from this Tools menu in VS 2010:

image

That’s it in Part 1 for our prep-work. I’ll start with the List definition in Part 2.

Saturday, October 6, 2012

Adding Content to a SP 2010 Master Page

 

There are situations when we need some external content to be displayed in the master page, and at the same time allow users to easily update this content without much hassles of code-changes to the master page. I went hunting for the same and eventually identified a few of them. Here’s a compilation of what I felt beneficial to all. Let me know, if you have more on your list.

#1. You can create a user control and insert into the Master page footer. Click here for more details.

#2. You can insert the webpart directly into the Master page footer. Let’s see how.

<table cellpadding="0" cellspacing="0" width="875" align="left">
  <tr><td>
      <WebPartPages:ContentEditorWebPart AllowClose="true" AllowEdit="false" AllowHide="false"
                    AllowMinimize="false" AllowRemove="false" AllowZoneChange="false" ChromeType="None"
                    ContentLink="/Documents/Footer.htm" ID="vha_simple_footer" runat="server"
                    __WebPartId="{9AADFFEC-FA65-4BB7-BAE6-4DA3A85A56A0}" WebPart="true"></WebPartPages:ContentEditorWebPart>
   </td></tr>
  </table>

#3. In case your site pages are all based upon few Page Layouts, you “might” place some ContentPlaceHolders on these page-layouts, and then add the footer code into a content editor web part on a page that is based upon the Page Layout. Refer this article from Bamboo Solutions for a complete details on creating a custom page layout for a Publishing Site using Visual Studio 2010

#4. You may also use “Reusable Content” in SP Publishing HTML fields to achieve the same. Find here.

#5. Then there are IFrames, the old ways of displaying a web page content. Well, I know, most of us would start a discussion around the SEO, when we talk about IFrames on the pages. But it still works and provides another way to get external content into a master page. (Although, I personally know, how powerful iframes programing can be. Check here.)

This is how to get this in place. Insert the below piece of code, in the footer section of the master page:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr><td>
       <iframe src="/Documents/Footer.htm" scrolling="no" frameborder="0">[Your browser does not support frames or is currently configured not to display frames. Please use an up-to-date browser that is capable of displaying frames.]</iframe>
</td></tr></table>

Next, upload the “Footer.htm” file with appropriate code in the [Documents] document library of the root site. I have a sample code for this file as:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<div align="center"><span style="font-size:10pt">External footer content example</span></div>  </body>
</html>

I agree, this one is not the best of the practices, and I would advice not to consider this as a first hand solutions when other options might be more suitable for similar scenarios.

Sunday, September 23, 2012

Site Image Logo in SP 2010

 

Seems very simple, when it comes to setting up a Logo Image for your SP 2010 Site and its sub-site.

Use the LogoImageUrl to set the path of the image (say in “Layouts” folder) in the <SharePoint:SiteLogoImage> as seen below:

<SharePoint:SPLinkButton runat="server" NavigateUrl="~sitecollection/">
             <SharePoint:SiteLogoImage LogoImageUrl="/_layouts/mysitename/my_site_logo.gif" runat="server"/>
</SharePoint:SPLinkButton>

But things get a little complicated, if a Site Admin sets a different image in the [Site Settings] – [Title, Description and Icon] under “Look and Feel”.

image

It seems that this property tends to override the master-page LogoImageUrl in the <SharePoint:SiteLogoImage> as shown in the code above.

So the Solution:

- Clear the [Title, Description and Icon] field under “Look and Feel” section in [Site Settings]. I mean set it to blank or update it to the same image as set in the master-page.

But what if you have 100s of sites/sub-sites (in my case, there were 1500 sub-sites).

Power-shell to our rescue….

PS script can help us do the update needed very conveniently. That’s why we all love PS in SP 2010.

So here it is. Simply replace your own site URL and the Image path and run this script.

$site = Get-SPSite http://www.mysitename.com
$site.AllWebs | foreach { $_.SiteLogoUrl = "/_layouts/mysitename/my_site_logo.gif"; $_.Update() }
$site.Dispose()

That’s all we need to do.Have a nice day !!!

Saturday, September 8, 2012

Custom 404 Page Not found in SharePoint 2010

 

Well, now that we have successfully upgraded the MOSS 2007 sites to SharePoint 2010, there was a slight change in the URL structure in the new environment. In 2010, the site collection was moved to the top root level, more details on how to do this can be found here, as described below:

MOSS 2007: http://www.domianname.com/sitename/Pages/Home.aspx

SP 2010: http://www.domianname.com/Pages/Home.aspx

The issues that were identified:

#1. Bookmarked URLs might break for current users.

#2. The embedded deep-links in various email notifications already sent, may not work anymore.

#3. The various links to many pages, made available on other sites may start breaking with a page not found error.

The quick resolution that was agreed upon:

To implement a generic, custom page that should be displayed each time a user hits a 404 page not found error. This page would have a generic message and a link to take the user back to the home page.

How we did this:

Step 1: Create a meaningful 404CustomPage in the Site-Collection

a. Open-up the site and create a new Web-Part page in the Pages Library. Call it :”MyCustom404.aspx”.

b. Note the page URL; it should be “/Pages/MyCustom404.aspx”.

c. Design the page as per the business requirements by inserting Content Editors and Image web parts.

d. Place a generic but meaningful text on a content editor along with a link pointing back to, say, Home page.

Step 2: Create the Web Config Entries for the Web Application

a. Open-up the web-config file for the web application.

b. Create an entry for <httpErrors> section within the <system.webServer> as below:

<httpErrors errorMode="Custom" existingResponse="Auto">

<remove statusCode="404" subStatusCode="-1" />

<error statusCode="404" prefixLanguageFilePath="" path="/Pages/MyCustom404.aspx" responseMode="ExecuteURL" />

</httpErrors>

c. Save the config file and do an IISRESET.

Now go back and try to browse to a non-existing page on the site and you should see the custom 404 page being displayed.

Improvements that I can think of:

1. We can have a redirect logic on our custom 404 page to programmatically fix the url and redirect the user to the new page url.

2. We can also create a SP 2010 Solution package to automate the process of updating the web config file. This really helps in a multi-environment deployments by the admins.

I hope to get this improvements in-place in the next couple of weeks. I’ll update this post with the link to my new post so till then, have a nice time.

Saturday, August 25, 2012

Conditional Javascript in QuickLaunch bar

 

This is a great help !

The ability to display some conditional pop-ups/dialog boxes to the end-users when they click on various links on the Quick Launch bar items, seems a great idea.

I was looking to present a message to the users, whenever they clicked a certain hyper-link on the left navigation.

I wanted them to understand, that they are navigating away from their current site to a page url that might be externally hosted.

This is what really helped me do the same:

javascript: if(confirm(‘You are being re-directed to a site hosted externally. Click OK to proceed, Cancel to stay on the page.’)) document.location=’http://www.bing.com’;

Note: The above line of code being typed in a single line.

Thanks to my colleague Mike Smith for posting this trick with many other options as well.

Friday, January 7, 2011

Working with PowerShell “cmdlets”

Pronounced “Command-lets”, cmdlets are a great way of scripting Administrative tasks in SharePoint 2010. I personally feel that I was missing such a tool till last year working with MOSS 2007. But with cmdlets, it looks extremely promising that Microsoft has ultimately provided the SharePoint Administrators – a single platform to script their tasks and run commands remotely/locally.

Recently, I was trying to look into these cmdlets and here are some quick view into some of the most basic ones.

Interestingly, these cmdlets are in a format that resembles the verb-noun pair. The noun generally starting with a “SP” for all SharePoint cmdlets.

#1. Get-SPContentDatabase: This gives you a list of all the Content DBs in your farm, with information on ID, Name, Server and Site count.

Okay, now you must be thinking that I am going to get nasty by listing down all these cmdlets, uhh… you are definitely wrong.

Instead, I just figured-out how to retrieve a list of such SP cmdlets and also how to pull more information about each of them.

Get-Command and Get-Help can be really handy for anyone starting on these. Here’s an example of how to use the Get-Command.

Get-Command –PSSnapin “Microsoft.SharePoint.PowerShell”

Further-more, if you start creating your own scripts, you would also need to start using these two very often:

> Measure-Command: This gives you the cmdlets execution time.

> Trace-Command: gives you the ability for debugging short scripts.

Technorati Tags: ,,