• Using _spPageContextInfo, AJAX and SAMAccountName to show a hidden item in a SharePoint page.

    I wanted to have a link on a page (could be anything) show only for certain people. I knew there was variable on every page named _spPageContextInfo.userId, which is the current user’s SharePoint user Id. I used the following code to take that userId and query the User Information List to get the SAMAccountName, and then use jQuery to show a hidden link if the use name matches.

        var userId = _spPageContextInfo.userId;
        var SAMAccountName = ''
        var serverUrl = "/_vti_bin/listdata.svc/UserInformationList(" + userId + ")"
    
        var URL = serverUrl
        $.ajax({
            type: "GET",
            contentType: "application/json",
            datatype: "json",
            async: false,
            url: URL,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                XMLHttpRequest.setRequestHeader("Content-Type", "application/json")
            },
            success: function (data, textStatus, XmlHttpRequest) {
                SAMAccountName = data.d.UserName
            }
        });
        if (SAMAccountName.toLowerCase() === 'UserName1'
            || SAMAccountName.toLowerCase() === 'UserName2'
            || SAMAccountName.toLowerCase() === 'UserName3'
            || SAMAccountName.toLowerCase() === 'UserName4'
            ) {
            $('#HiddenId').show()
        }
    

  • Looping through a SharePoint List column and acting on each item in the column using jQuery

    I wanted to loop through a couple columns in the Default View of a SharePoint List. The jQuery code below would loop through the 4th, 5th, and 10th columns and alert the contents of that column. Obviously you can do anything you like with the matches. More tomorrow on how I was using this method

    $('table.ms-listviewtable td:nth-child(4),table.ms-listviewtable td:nth-child(5),table.ms-listviewtable td:nth-child(10)').each(function () {
    
    alert($(this).Text())
    }
    

  • PowerShell: foreach with a first and last item number

    I wanted to work through 1000+ items in a PowerShell foreach loop, but I wanted to do it X at a time. I figured out the following foreach syntax to loop through all items after first and before last:

    $First = 15
    $Last = 45
    foreach ($Row in $Rows | select -first $Last | select -last (($Last - $First)+1)){
    . . . .
    }
    

  • Knockout alternate formattting

    I was using KnockoutJS to loop through and display some data. I wanted to apply some alternate formatting on every other row. I used this syntax containing the MOD operation to achieve the formatting I wanted.

    <!-- ko if: $index() % 2 === 0 -->
    

    The other thing I learned was that $index() is observable, so I can +1 it? I believe that is right.

    The following were some sources that clued me in:

    http://jsfiddle.net/KuJBv/11/
    https://groups.google.com/forum/#!msg/knockoutjs/ElVix0ksXh8/awkTFYewitAJ


  • Using PowerShell to add a Contact to a CRM 2011 MarketingList (SOAP)

    We had a user delete a Marketing List. I needed to recreate it. I went to a database backup and found the GUID of the deleted list.
    Then I used the following SQL query to find the GUIDs of all the members of that list:

    SELECT FullName
        ,ParentCustomerIdName
        ,[EntityId]
        ,[ListId]
        ,[ListMemberId]
      FROM [CRMDataBaseName].[dbo].[ListMember],[CRMDataBaseName].[dbo].Contact
      where ListId = '787b77ca-c47d-431b-863e-12a98969b097' AND 
      [EntityId] = ContactId
      order by LastName,FirstName
    

    I saved the EntityId column to a text file, and then I used the following PowerShell code to loop through the GUIDs and add them to a new MarketingList

    
    $ListMembers = Get-Content C:\IT\Temp\ListMemberGUIDs.txt
    foreach ($EntityId in $ListMembers){
    $xml = ""
    $xml += "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>";
    $xml += "  <s:Body>";
    $xml += "    <Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>";
    $xml += "      <request i:type='b:AddMemberListRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>";
    $xml += "        <a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>";
    $xml += "          <a:KeyValuePairOfstringanyType>";
    $xml += "            <c:key>ListId</c:key>";
    $xml += "            <c:value i:type='d:guid' xmlns:d='http://schemas.microsoft.com/2003/10/Serialization/'>5deb4efb-4ed7-47f3-8e8e-bb487e0db423</c:value>";
    $xml += "          </a:KeyValuePairOfstringanyType>";
    $xml += "          <a:KeyValuePairOfstringanyType>";
    $xml += "            <c:key>EntityId</c:key>";
    $xml += "            <c:value i:type='d:guid' xmlns:d='http://schemas.microsoft.com/2003/10/Serialization/'>$($EntityId)</c:value>";
    $xml += "          </a:KeyValuePairOfstringanyType>";
    $xml += "        </a:Parameters>";
    $xml += "        <a:RequestId i:nil='true' />";
    $xml += "        <a:RequestName>AddMemberList</a:RequestName>";
    $xml += "      </request>";
    $xml += "    </Execute>";
    $xml += "  </s:Body>";
    $xml += "</s:Envelope>";
     
    $url="http://crm.sardverb.com/SardVerbinnen/XRMServices/2011/Organization.svc/web"
     
    $http_request = New-Object -ComObject Msxml2.XMLHTTP
    $http_request.Open('POST', $url, $false)
    $http_request.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute")
    $http_request.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
    $http_request.setRequestHeader("Content-Length", $xml.length)
    $http_request.send($xml)
    }
    
    
    

  • I am at SharePoint Conference 2012 !!! #SPC12

    I can’t wait for the sessions to start. Although seeing all these smart people is a little intimidating.

    20121112-082054.jpg


  • 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"
    	}
    }
    

  • Install-SPSolution, Add-SPSolution and Update-SPSolution return error: not supported with version 4.0.30319.269 of the Microsoft .Net Runtime.

    Just tried to install a soliution, and I received the error:

    Install-SPSolution : Microsoft SharePoint is not supported with version 4.0.30319.269 of the Microsoft .Net Runtime.

    I just installed the new PowerShell 3.0. To fix, launch a new powershell with -Version 2, and then run your command again:

    powershell -version 2

    That is what I get for installing the newest and the greatest!