Tuesday, October 20, 2015

PowerShell: SharePoint 2010 Add top/Global navigation links and sublinks

This works for SharePoint 2010 sites with the Publishing feature enabled.
Before getting started, populate the header, title and URLs in an XML file (I am saving this in a file called topNav.xml).

Sample format:

 <Navigation>  
      <Group Title="Sites">  
           <Link>  
                <Name>Asia Pacific</Name>  
                <Url>http://asiapac</Url>  
                <Target />  
           </Link>  
           <Link>  
                <Name>Canada</Name>  
                <Url>http://canada</Url>  
                <Target />  
           </Link>  
           <Link>  
                <Name>Europe</Name>  
                <Url>http://eu</Url>  
                <Target />  
           </Link>  
           <Link>  
                <Name>Japan</Name>  
                <Url>http://japan</Url>  
                <Target />  
           </Link>  
           <Link>  
                <Name>Penang</Name>  
                <Url>http://penang</Url>  
                <Target />  
           </Link>  
           <Link>  
                <Name>United States</Name>  
                <Url>http://states</Url>  
                <Target />  
           </Link>  
      </Group>  
 </Navigation>  

And here is the powerShell script to grab the values and populate the Global Navigation section.

Thursday, October 15, 2015

PowerShell: Update target Audience of a SharePoint 2010 Web Part

 # Get reference to the SPSite object  
 $SPWeb = Get-SPWeb "http://sharepoint"  
 # Get reference to the landing page. Since it is a publishing site so default.aspx is the landing page   
 $page = $SPWeb.GetFile("Pages/index.aspx")   
 ### Uncomment the lines below to perform the update  
 #$page.CheckOut()   
 # //Get reference to the webpartmanager class   
 $webpartmanager = $SPWeb.GetLimitedWebPartManager("Pages/index.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    
 # Double check the web part that you wish to update. Iterate through webparts in webpartmanager class   
 for ($i=0;$i -lt $webpartmanager.WebParts.Count;$i++)   
 {    
      #write-host $webpartmanager.WebParts[$i].title  
      # Check for the name of required web part    
      if($webpartmanager.WebParts[$i].title -eq "Content Query Web Part")    
      {    
           #Get reference to the web part   
           $wp = $webpartmanager.WebParts[$i];   
           write-host $wp.title " " $i  
           # If there is any existing filter, copy it  
           write-host $wp.AuthorizationFilter  
           # In the filter (empty: ;;;;;;), first two semicolons: individual users as target audience; second two semicolons: distribution list; last two semicolons: can be replaced to include sharepoint groups    
           # e.g. "174e7e9d-ff39-4856-85a6-9247b02e59fb,124f1234-3998-4b54-83d1-abc4048fb9b4;;;;"  
           ### Optionally perform direct update while in for loop  
           # Other properties e.g. chrome property   
           #$wp.ChromeType="TitleAndBorder";   
           # Save changes to webpartmanager. This step is necessary. Otherwise changes wont be reflected   
           #$webpartmanager.SaveChanges($wp);   
  }    
  }    
 ### Uncomment the lines below to perform the update  
 #$wp = $webpartmanager.WebParts[26];  
 #write-host "begin update: " $wp.Title  
 #$wp.AuthorizationFilter = "174e7e9d-ff39-4856-85a6-9247b02e59fb,124f1234-3998-4b54-83d1-abc4048fb9b4,3a87dc31-9067-4996-b4c2-5efc6ff3eab7,68d2a840-bdbb-44ee-aacd-395b5551c473,7add71c2-dbea-4218-b8ff-f6b112f225e4,577550be-616e-41cd-90a4-493c030ddd72,9f113155-7c70-4d18-9417-851d82ba8b6a,c664006b-ec7f-4798-9acc-c48766be68a8,e26f7ece-615c-474d-9db9-0f60953e113a,ee912564-4f77-43c4-aced-55d387bc4e8e,51d2b9df-7ca3-42ad-a307-f9fdf28fe417,59500291-5912-47c0-b6a2-9c9adf24f2da;;;;";  
 #$webpartmanager.SaveChanges($wp);  
 #write-host "saving webpart target audience done...";  
 ### Uncomment the lines below to check in and Publish the page   
 #$page.CheckIn("Update Audience(batch powershell)")   
 #$page.Publish("Update Audience batch powershell)")   
 #$SPWeb.Update();    
 # Dispose SPWeb object   
 $SPWeb.Dispose();   

Wednesday, September 30, 2015

PowerShell list SharePoint Global Navigation headers and subheader links

This script will traverse the top navigation bar links in a SharePoint site and retrieve all the main headers plus dropdown links, and optionally list the link URLs.

Tried on SharePoint publishing sites.
How do you know if publishing is turned on?
If you see the Navigation link instead of Top Link Bar / Quick Launch, the feature is turned on.


Wednesday, September 9, 2015

SharePoint PowerShell: Adding complex rules to existing audience

Here is a PowerShell script to modify an existing Audience.

As you know, in SP2010, audience rules configured from the Central Administration can only have 2 choices:
1. Meet any condition listed
2. Meet ALL conditions listed

So if I want to have more complex conditions like nested conditions, I will either need to write a program (C#) to do it, or run some powershell commands in the web front end.

DO take note that once an audience is programmatically modified, you WILL NOT BE ABLE TO MAKE CHANGES from the Central Administation console anymore.

Sample script below:


Wednesday, August 26, 2015

SQL Find stored procedure containing text

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%Foo%' 
    AND ROUTINE_TYPE='PROCEDURE'

SELECT OBJECT_NAME(id) 
    FROM SYSCOMMENTS 
    WHERE [text] LIKE '%Foo%' 
    AND OBJECTPROPERTY(id, 'IsProcedure') = 1 
    GROUP BY OBJECT_NAME(id)

SELECT OBJECT_NAME(object_id)
    FROM sys.sql_modules
    WHERE OBJECTPROPERTY(object_id, 'IsProcedure') = 1
    AND definition LIKE '%Foo%'
Copied from
http://stackoverflow.com/questions/5079457/how-do-i-find-a-stored-procedure-containing-text

Wednesday, July 22, 2015

Super AWESOME! SQL find value from ALL tables

This great script was originally posted by fullparam. Read the original post here

I did some modifications because I had to run it on SQL Server 2000 - It's obsolete I know!


DECLARE @SearchStrTableName nvarchar(255), @SearchStrColumnName nvarchar(255), @SearchStrColumnValue nvarchar(255), @SearchStrInXML bit, @FullRowResult bit, @FullRowResultRows int
SET @SearchStrColumnValue = '%1k9ovp.q%' /* use LIKE syntax */
SET @FullRowResult = 1
SET @FullRowResultRows = 3
SET @SearchStrTableName = NULL /* NULL for all tables, uses LIKE syntax */
SET @SearchStrColumnName = NULL /* NULL for all columns, uses LIKE syntax */
SET @SearchStrInXML = 0 /* Searching XML data may be slow */

IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
CREATE TABLE #Results (TableName nvarchar(128), ColumnName nvarchar(128), ColumnValue nvarchar(2000),ColumnType nvarchar(20))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256),@ColumnName nvarchar(128),@ColumnType nvarchar(20), @QuotedSearchStrColumnValue nvarchar(110), @QuotedSearchStrColumnName nvarchar(110)
SET @QuotedSearchStrColumnValue = QUOTENAME(@SearchStrColumnValue,'''')
SET @TableName = ''
--DECLARE @ColumnNameTable TABLE (COLUMN_NAME nvarchar(128),DATA_TYPE nvarchar(20))
IF OBJECT_ID('tempdb..#ColumnNameTable') IS NOT NULL DROP TABLE #ColumnNameTable
CREATE TABLE #ColumnNameTable (COLUMN_NAME nvarchar(128),DATA_TYPE nvarchar(20))
WHILE @TableName IS NOT NULL
BEGIN
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME LIKE COALESCE(@SearchStrTableName,TABLE_NAME)
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
)
IF @TableName IS NOT NULL
BEGIN
DECLARE @sql VARCHAR(2000)
SET @sql = 'SELECT QUOTENAME(COLUMN_NAME),DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(''' + @TableName + ''', 2)
AND TABLE_NAME = PARSENAME(''' + @TableName + ''', 1)
AND DATA_TYPE IN (' + CASE WHEN ISNUMERIC(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@SearchStrColumnValue,'%',''),'_',''),'[',''),']',''),'-','')) = 1 THEN '''tinyint'',''int'',''smallint'',''bigint'',''numeric'',''decimal'',''smallmoney'',''money'',' ELSE '' END + '''char'',''varchar'',''nchar'',''nvarchar'',''timestamp'',''uniqueidentifier''' + CASE @SearchStrInXML WHEN 1 THEN ',''xml''' ELSE '' END + ')
AND COLUMN_NAME LIKE COALESCE(' + CASE WHEN @SearchStrColumnName IS NULL THEN 'NULL' ELSE '''' + @SearchStrColumnName + '''' END  + ',COLUMN_NAME)'
INSERT INTO #ColumnNameTable --@ColumnNameTable
EXEC (@sql)
WHILE EXISTS (SELECT TOP 1 COLUMN_NAME FROM #ColumnNameTable)--@ColumnNameTable)
BEGIN
PRINT @ColumnName
SELECT TOP 1 @ColumnName = COLUMN_NAME,@ColumnType = DATA_TYPE FROM #ColumnNameTable--@ColumnNameTable
SET @sql = 'SELECT ''' + @TableName + ''',''' + @ColumnName + ''',' + CASE @ColumnType WHEN 'xml' THEN 'LEFT(CAST(' + @ColumnName + ' AS nvarchar(MAX)), 4096),'''
WHEN 'timestamp' THEN 'master.dbo.fn_varbintohexstr('+ @ColumnName + '),'''
ELSE 'LEFT(' + @ColumnName + ', 4096),''' END + @ColumnType + '''
FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + CASE @ColumnType WHEN 'xml' THEN 'CAST(' + @ColumnName + ' AS nvarchar(MAX))'
WHEN 'timestamp' THEN 'master.dbo.fn_varbintohexstr('+ @ColumnName + ')'
ELSE @ColumnName END + ' LIKE ' + @QuotedSearchStrColumnValue
INSERT INTO #Results
EXEC(@sql)
IF @@ROWCOUNT > 0 IF @FullRowResult = 1
BEGIN
SET @sql = 'SELECT TOP ' + CAST(@FullRowResultRows AS VARCHAR(3)) + ' ''' + @TableName + ''' AS [TableFound],''' + @ColumnName + ''' AS [ColumnFound],''FullRow>'' AS [FullRow>],*' +
' FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + CASE @ColumnType WHEN 'xml' THEN 'CAST(' + @ColumnName + ' AS nvarchar(MAX))'
WHEN 'timestamp' THEN 'master.dbo.fn_varbintohexstr('+ @ColumnName + ')'
ELSE @ColumnName END + ' LIKE ' + @QuotedSearchStrColumnValue
EXEC(@sql)
END
DELETE FROM #ColumnNameTable /*@ColumnNameTable*/ WHERE COLUMN_NAME = @ColumnName
END
END
END
SET NOCOUNT OFF

SELECT TableName, ColumnName, ColumnValue, ColumnType, COUNT(*) AS Count FROM #Results
GROUP BY TableName, ColumnName, ColumnValue, ColumnType

Tuesday, June 23, 2015

SharePoint 2010 PowerShell: Batch assign permissions to each list item

PowerShell script to add permissions to all items in a list or library, where each item already has its unique set of permissions. Useful when you have hundreds of items in your library and can't be bothered to assign one by one.

Tuesday, June 16, 2015

SharePoint 2010 "Add to My Links" error: Page not found

Today I came across an interesting issue where users from a certain remote location were reporting a 'Page not found' error when trying to use 'Add to My Links' for documents stored under mysites.

Interestingly, the URL was malformed each time they attempted to click on the function.
It would end up looking like http://_vti_bin/portalapi.aspx?cmd=PinToMyPage&IconUrl=/_layouts/images/null&ListViewURL=xxxxxxxx 

Wednesday, June 3, 2015

SharePoint 2010 - Where are custom .acsx stored?

If you are developing visual webparts in SharePoint 2010 you may have wondered where the controls were stored.
It can be found in this folder in the 14 hive:
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\

However for SharePoint 2013 this is no longer valid as the ascx is generated on the fly from the compiled DLL.

Monday, February 23, 2015

SharePoint PowerShell - Set Site Collection Owner

Set-SpSite "http://sharepoint" -owneralias "DOMAIN\username"
Set-SpSite "http://sharepoint" -SecondaryOwnerAlias "DOMAIN\username2"

or alternatively, set the secondary site collection owner to null.

Set-SpSite "http://sp2010:90" -SecondaryOwnerAlias $null

Sunday, February 15, 2015

Upgrading instances of a form - InfoPath 2010 administrator-approved templates

If you have an administrator approved template for InfoPath in SharePoint, you will notice that whenever you upload a new form template via Central Administrator, all existing instances of the form would not be automatically upgraded.

If there are specific scenarios where you need to have existing forms upgraded, there is a very simple workaround.
1. Take note of the most recent uploaded version of the form in Central Administration - General Application Settings - Manage form templates.
2. Download a copy of the form instance.
3. Open the .xml file in a plain text editor.
4. Modify the version in the form's solutionVersion attribute near the header.
 <?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Your-Form-Name:-myXSD-2014-01-27T23-22-27" solutionVersion="1.0.0.155" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="http://Your-Site/Your-Form-Name.xsn"?>  
5. Save and re-upload the .xml with the same name.

Wednesday, February 11, 2015

五分埔 Wufenpu and Raohe Night Market 饒河街觀光夜市

This is part of the fun Taiwan travel series. View the full itinerary here.

Finally after almost 1 year, I dredged up the energy to bring the Taiwan travel episodes to an end!

On my 9th day of the free and easy trip to Taiwan, we made a short but unavoidable trip to the famous Wufenpu 五分埔 - the place to go for sourcing cheap fashion bargains. It is the largest clothing wholesale market that could be found in Taipei.

 We read from other blogs not to go too early as most shops generally open after noon, so we dropped by the area around 3PM. It is very easy to locate Wufenpu. Just follow the crowd!

After alighting from the Houshanpi MRT exit, we crossed the street and looked for the shop selling braised pork rice called Formosa Chang. It's quite famous as it has been mentioned extensively in travel blogs; however we opted to skip it and had lunch somewhere else.


SharePoint PowerShell: Recursively checkin files in a folder

This is a script that will try to parse a certain folder in a SharePoint 2010 site and recursively check for files that are checked out.
If found, the files would be checked in,

This is useful in situations where you are unable to move files and folders because someone had the files checked out.

Sunday, February 1, 2015

AgilePoint InfoPath C# snippet - Retrieve repeating row data

Sometimes we would need to manipulate data from an InfoPath repeating field, use this data in our AgilePoint workflows. We would need to add custom code for this by using the Advanced Extensions - Managed Code stencil.

Sample of InfoPath schema:



I have a repeating row called Item with 3 fields or nodes.

My example would be to invoke a subprocess for each row of that data.
In order to access the data, I would need the System.Xml and System.Xml.XPath references in my code. I will make use of the XPathNavigator and XPathIterator functions to get data.

Related Posts Plugin for WordPress, Blogger...