Tag Archives | SharePoint 2010

A better jQuery selector to hide a row in a SharePoint 2010 Edit Form

In the past, I had used this code to hide a row in a SharePoint Edit Form (EditForm.aspx):

$('nobr:contains("Attendees")').closest('tr').hide();

The problem with this is that if you have two fields that contain the text “Attendees” (For example “NewAttendees” & “OldAttendees”), then it matches both of them (this always seems to to be an issue for us). So I did a little research and I found a better way to select a row (I think this was my original source)

Here is my new/better way to match a row:

 $('nobr').filter(function () { return $(this).text() === 'Attendees' }).closest('tr').hide();

If it was a required field, it would be:

 $('nobr').filter(function () { return $(this).text() === 'Attendees *' }).closest('tr').hide();

Hope that helps someone.

Using jQuery to change to the Items tab in a SharePoint 2010 list.

In my last post, I showed how to use a query string parameter to start a user out on the items tab of a SharePoint list. This is well documented, I just can’t seem to remember it (that is why I posted it!). I wanted to take it a step further and use jQuery to change to the Items tab. Here is the code to do that:

$(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(function () {
          _ribbonStartInit("Ribbon.ListItem", false, null);
    }, "sp.ribbon.js");    
});

The trick was to make sure the sp.ribbon.js script was loaded, and then use the included function to change to the preferred tab. Hope that helps someone.

Replace the New Item button on a SharePoint 2010 list with jQuery

I have a SharePoint 2010 Business Data Catalog (BDC) pointing to our MS CRM 20111 back end. This is a good way to show paged views of the database. But at the top, there is still a “New Item” button. This does not make much sense for a BDC (or External list) without update queries. I wanted a workaround. I wanted to “hijack” this button – Make it do what I want. This is the jQuery code to do that!
Basically I am hiding the existing button, and replacing it with what I want.

    $("#RibbonContainer").ready(function () {
        var imageRow = '<span class="ms-cui-ctl-large" onclick=\"window.location=\'/Destination/YouWant/NewForm.aspx?source=/Lists/BDCList/Default.aspx?InitialTabId=Ribbon.ListItem\'; \") > \
            <a class="ms-cui-ctl-a1 "  href="javascript:;" > \
            <span class="ms-cui-ctl-a1Internal" unselectable="on"> \
            <span class=" ms-cui-img-32by32 ms-cui-img-cont-float" unselectable="on"> \
            <img class="" style="left: -64px; top: -320px;" src="/_layouts/1033/images/formatmap32x32.png" /> \
            </span> \
            </span> \
            </a> \
            <a class="ms-cui-ctl-a2"><span class="ms-cui-ctl-largelabel"> New <br/> Item </span></a> \
            </span>'
        $("#s4-ribboncont .ms-cui-ctl-large:contains('New Item')").hide()
        $("#s4-ribboncont .ms-cui-ctl-large:contains('New Item')").before(imageRow)
    }); 

PowerShell command to allow inline PDF viewing in SharePoint 2010

My users like to view PDFs in their browser on our SharePoint site. I needed to allow this in 2010:
Here is the powershell to allow inline PDF viewing in SharePoint 2010

$webapps = Get-SPWebApplication "SharePoint - intranet.company.com80"
foreach ($webapp in $webapps) 
{ 
    $webapp.AllowedInlineDownloadedMimeTypes.Add("application/pdf") 
    $webapp.Update() 
}

Using jQuery to add a new item to the breadcrumbs at the top of a SharePoint 2010 site.

I wanted to add a bread crumb item to a sub site that pointed back to the home page. This is the jQuery code I used to insert it. This code also aadds the little arrow divider


var NavItem ='<a id="ctl00_PlaceHolderSiteName_onetidProjectPropertyTitle" href="/">Home</a>'

NavItem = NavItem + '<span id="onetidPageTitleSeparator" class="s4-nothome s4-bcsep s4-titlesep"><span><span style="height:11px;width:11px;position:relative;display:inline-block;overflow:hidden;"><img src="/_layouts/images/fgimg.png" alt=":" style="border-width:0px;position:absolute;left:-0px !important;top:-585px !important;" /></span></span> </span>'

$(".s4-titletext h1").prepend(NavItem)

PowerShell 3: Invoke-WebRequest vs Invoke-RestMethod and a SharePoint 2010 list with more than 1000 entries

When using Invoke-RestMethod with a SharePoint 2010 list and ListData.svc, it returns an “System.Xml.XmlElement#http://www.w3.org/2005/Atom#entry” object. Not sure why, but the end result is that you can’t get access to the “rel=next” url, or am I doing something wrong?

$Results=Invoke-RestMethod -uri $ListUrl -UseDefaultCredentials
$Results | gm 
TypeName: System.Xml.XmlElement#http://www.w3.org/2005/Atom#entry

I had to use Invoke-WebRequest and then take the Content and put it in an XML variable, only then could I get to the next page of items.

$MailingLabels = Invoke-WebRequest -Uri $ListUrl -UseDefaultCredentials
$Next =  ($MailingLabelsXML.feed.link | ?{$_.rel -eq "next"}).href

Thoughts?

SharePoint 2010 modal dialog (showModalDialog) without an existing page

I was retrieving Activity data from a Microsoft CRM 2011 REST query. I wanted to have a popup with more information. I decided to use the built in showModalDialog. The problem was that all the examples I found showed how to popup an existing page. I wanted the modal to contain data that didn’t exist anywhere. The solution was to use the following code, specifically create a divElement and set the innerHTML to html that contained the data I want to show. I threw in some typical SharePoint css classes to keep the same look and feel as the rest of the site.

    displayActivityModalDialog = function () {
        var divElem = document.createElement('div');
        var htmlOutput = '<div class="ms-bodyareacell"><table>'
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Type:</td><td class="ms-formbody">' + this.ActivityType + '</td></tr>';
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Subject:</td><td class="ms-formbody">' + this.Subject + '</td></tr>';
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Date:</td><td class="ms-formbody">' + this.ScheduledStart + '</td></tr>';
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Regarding:</td><td class="ms-formbody">' + this.Regarding + '</td></tr>';
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Organizer:</td><td class="ms-formbody">' + this.Organizer + '</td></tr>';
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Description:</td><td class="ms-formbody">' + this.Description + '</td></tr>';
        htmlOutput += '<tr><td width="190" class="ms-formlabel">Other Attendees:</td><td class="ms-formbody">' + this.RequiredAttendee + '</td></tr>';
        htmlOutput += '</table></div>'

        divElem.innerHTML = htmlOutput;
        var options = SP.UI.$create_DialogOptions();
        options.html = divElem;
        options.title = "Activity details";
        options.showClose = true;
        options.showMaximized = false;
        SP.UI.ModalDialog.showModalDialog(options);
    }

Now I can put anything in a showModalDialog!

PowerShell script to backup all SharePoint 2010 lists in all webs in all sites

More backups the better. I wanted a file level backup of every list. Below I used PowerShell to iterate through all the lists on the server and dump them into a folder

$backupDir="c:\Temp"
foreach ($web in $(Get-SPSite | Get-SPWeb)){
	foreach ($list in $web.Lists) {
	mkdir -force "$backupDir\$($Web.Title.replace(' ',''))\"
	Export-SPWeb $($web.Url) -itemurl "$($list.RootFolder.ServerRelativeUrl)" -path "$backupDir\$($Web.Title.replace(' ',''))\$($list.Title.replace(' ','')).cmp"
	}
}