Friday, February 17, 2012

Sharepoint 2010 - Advanced Settings for Document library in SharePoint 2010 using PowerShell

In this article we will be seeing how to change the Advanced Settings for Document library in SharePoint 2010 using PowerShell and c#.

Go to Document Library => Library Settings => General Settings =>Advanced Settings.

DocShare1.gif

Using C#:
using (SPSite site = new SPSite("http://serverName:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList docLibrary=web.Lists["Doc Library"];

                    // Change the advanced settings
                    // Update the changes
                    docLibrary.Update();
                }
            }

Using PowerShell
$site=Get-SPSite "http://serverName:1111/"
$web=$site.RootWeb
$docLibrary =$web.Lists["Doc Library"]
# Change the advanced settings
$docLibrary.Update()
Content Types:

DocShare2.gif

C#:
docLibrary.ContentTypesEnabled = false;

PowerShell:
$docLibrary.ContentTypesEnabled = $false

Opening Documents in the Browser:DocShare3.gif

C#:
// Open in the client applicationdocLibrary.DefaultItemOpen = DefaultItemOpen.PreferClient;
// Open in the browserdocLibrary.DefaultItemOpen = DefaultItemOpen.Browser;
// Use the server defaultdocLibrary.DefaultItemOpenUseListSetting = false;

PowerShell:

#Open in the client application
$docLibrary.DefaultItemOpen = "PreferClient"

#Open in the browser
$docLibrary.DefaultItemOpen = "Browser"

#Use the server default
$docLibrary.DefaultItemOpenUseListSetting = $false
Custom Send To Destination: 

DocShare4.gif

C#:
docLibrary.SendToLocationName = "Shared Documents";
docLibrary.SendToLocationUrl = "http://serverName:1111/Shared%20Documents/";


PowerShell:

$docLibrary.SendToLocationName = "Shared Documents";
$docLibrary.SendToLocationUrl = "http://serverName:1111/Shared%20Documents/";

Folders: 
DocShare5.gif

C#:
docLibrary.EnableFolderCreation = false;

PowerShell:
$docLibrary.EnableFolderCreation = $false

Search:

DocShare6.gif

C#:
docLibrary.NoCrawl = true;

PowerShell:
$docLibrary.NoCrawl = $true

Offline Client Availability:DocShare7.gif

C#:docLibrary.ExcludeFromOfflineClient = true;

PowerShell:

$docLibrary.ExcludeFromOfflineClient = $true

Site Assets Library: 

DocShare8.gif

C#:
docLibrary.IsSiteAssetsLibrary = false;

PowerShell:
$docLibrary.IsSiteAssetsLibrary = $false

Datasheet: 

DocShare9.gif

C#:
docLibrary.DisableGridEditing = true;

PowerShell:

$docLibrary.DisableGridEditing = $true
Dialogs: 

DocShare10.gif

C#:

docLibrary.NavigateForFormsPages = true;

PowerShell:

$docLibrary.NavigateForFormsPages = $true

Sharepoint 2010 - Filter Web Part with QueryString: an issue?

In Sharepoint 2010, when you want to filter a List Web Part with a Form Web Part, you find the simple way with querystring and XSLT query.

List web part shows 5 results per page, and we have 3 pages (so 15 items).
I search for title and I see 3 results in the first page, but I see link for the other 3 pages.
And navigating the other page, I see one more result in second page and the third is empty...!
Filter shows results for the page 1 (3 items), for the page 2 (1 item) and for the page 3 (0 items), but divided in 3 pages.

It seems filter "hides" results not matching from view, keeping pages.

I decided to use filter with querystring used by Sharepoint when you filter columns.


So the address bar shows this:


You can use javascript to retrieve value for FilterFieldN (the column name to filter, internal name!) and FilterValueN (value to use in filter).

In this way, List Web Part filtered is in the right view: if I filter my 15 items, I found olny 3, but in one page.


Thursday, February 2, 2012

Sharepoint 2010 - Associate Workflow to List with Power Shell

I create a new Workflow Template (globally reusable) in root site and I associate it to a List in a Site Template.
But I need to update each sub site (created from my Site Template) with the new Workflow.

I use PowerShell, to cycle each subsite and associate WF to content type list.
This is command I used:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site=Get-SPSite "http://sharepoint_root_site"

foreach($web in $site.AllWebs){
   $list=$web.Lists["MyList"]
   $taskList=$web.Lists["Workflow Tasks"]
   $historyList=$web.Lists["Workflow History"]
   $ct=$list.ContentTypes["MyContenType"]
   $culture=New-Object System.Globalization.CultureInfo("en-US")
   $wfTemplate=$web.WorkflowTemplates.GetTemplateByName("wfTemplate", $culture)
   $associationWF=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($wfTemplate, "MyWFInSubSite", $taskList,$historyList)
   $ct.WorkflowAssociations.Add($associationWF)
   $ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)
}

It helps me for massive update.

Sharepoint 2010 - Filter List/Library Web Part with XSLT and Content Editor WP

I created a simple Filter Web Part with a Content Editor WP and simple XLST to filter List/Library with the LIKE feature.
It works for lists and libraries (in libraries not for Name field).

Create a page with Library Web Part to filter. Then add a Content Editor Web Part end edit HTML Source.
In HMTL, add an input textbox and two buttons, for reset filter and for submit filter.
Add some script as shown below:

<script type="text/javascript">

//this script get query string from URL
function querySt(ji){
  hu = window.location.search.substring(1);
  gy = hu.split("&");
  for (i=0;i<gy.length;i++){
    ft = gy[i].split("=");
    if (ft[0] == ji){
      return ft[1];
    }
  }
}

//this script set value read from query string in input field
function setValues(){
   if(querySt('qtitle') != null && querySt('qtitle') != "")
   {
     document.getElementById('query_title').value = querySt('qtitle');
   }
}

//this script reload page without filter in query string
function resetFilters(){
   document.location = "/SitePages/Search Documents.aspx";
}

//this script set query string and load page with filters
function getValues(){
   var el_query_title = document.getElementById('query_title');
   var value_query_title = el_query_title.value;
   window.location.href = "/SitePages/Search Documents.aspx?qtitle=" + value_query_title;
}

//this set values from query string after page is loaded (you can use your preferred method to do the same)
setTimeout('setValues();',800);

</script>

Title: <input name="query_title" id="query_title" type="text"/>
<input id="btn_reset" onclick="javascript:resetFilters();" type="button" value="Reset"/>
<input id="btn_filter" onclick="javascript:getValues();" type="button" value="Search"/>


Now, open Sharepoint Designer 2010 and edit page with filter.
Note: Edit page in Advanced Mode!

Click on List Web Part and in Ribbon Customizations, click on Customize XSLT (entire view).


In Ribbon Options, take care that column you want to filter is in view.
Now, Click on Parameter and add this parameters:


"qTitle", type Query String, with query string name qtitle (same as in the javascript above).
"lower", "upper" type None with value = "abcdefghijklmnopqrstuvwxyz" and "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Last two parameters are for tranform to Lower or to Upper text to filter: if I search for "None", I found in text contains "None", "NONE", "none".
We see how to later.

Now, in the code of library web part, search for tag :

<xsl:param name="AllRows" select="/dsQueryResponse/Rows/Row[($EntityName = '' or (position() &gt;= $FirstRow and position() &lt;= $LastRow))]"/>

and substitute with this:

<xsl:param name="AllRows" select="/dsQueryResponse/Rows/Row[((@Title != $qTitle and $qTitle = '*') or (contains(translate(@Title,$lower,$upper),translate($qTitle,$lower,$upper)) and $qTitle != '*')) and ($EntityName = '' or (position() &gt;= $FirstRow and position() &lt;= $LastRow))]"/>

Note the change we made.
The select take all rows with Title different from qTitle, if qTitle is wild card, or rows which Title contains qTitle, if qTitle is a word. This do the LIKE filter for text.
We add the translate function, to check only upper text of Title and upper text of qTitle.

Save your page (if asked for update display, say No: some time this overwrites your changes)

Now, go to browser on your search page, and try to filter web part.
Note for address, with query string and the result in web part.


You can add other filter with other column: columns must be displayed in view.

For the date, you have to convert them for correct XSL query.