Wednesday, December 28, 2011

Adding metadata properties in advanced search

While we were working with search, we created hiererchy, we created managed metadata properties and added refinements to our search on basis of those managed metadata properties.
Now, we want the same metadata properties in our advanced search. How to customise the advanced search webpart to view our metadata properties.

1. Go to the advanced search page, click on edit page
2. Click on 'Edit web part' on the Advance search box web part.
3. Expand the properties section.
4. Copy the xml in the properties section, paste to xml editor or visual studio.
5. Collapse all nodes.The last two nodes are the ones which we have to change.
6. First is the propertydefs node.
7. Copy and paste the below line in the property defs tag -
PropertyDef Name="Department" DataType="text" DisplayName="Department"
8. Now go to the 'ResultTypes' node and paste the below line in each section - PropertyRef Name="Department"
9. This line should go in all the sub sections of 'ResultTypes'.
10. Copy this xml and paste in the properties section of the web part.
11. Click on OK and come out of edit.

You will see the property as below in the advanced search section:

Tuesday, December 27, 2011

Redirect to custom search page instead of OSSSearchResults.aspx

While setting up search, we create a seperate search site. On this site, on the results page, we can set up any customizations we want. We can modify the xslt, set up refinements etc. On our default master page, the search box redirects us by default to the osssearchresults.aspx page. But, we want to be redirected to our custom search page.

A simple way to achieve this is to go to your site Collection, go to site settings - Under Site Collection administration, choose 'Search Settings'. On this page change the 'Site Collection Search Results Page' to 'http://.../search/results.aspx' or whatever is your custom search page.

Friday, December 2, 2011

Find feature name from id

There have been several cases, where I am trying to deploy something and it gives me an error saying 'Dependency of the below feature' etc. The error message just gives me a Feature Id. How to find out the name of the feature given the feature id.
The simplest way is to query the content database. Go to the database server of your sharepoint installation and type the below command:

use wss_content
select * from FeatureTracking where FeatureId like '8%'

Here feature id is the one in the error you are getting. You will get all the details of your feature. Use the content database of your particular web application.

Tuesday, November 15, 2011

Add Managed Metadata Property - Sharepoint search

Follow the below steps to add a custom managed metadata property.


1. Go to Central Administration, select Application management and select your shared services website.
2. In the search section, click search settings.
3. In the crawl settings section, click metadata property mappings.
4. Click New managed property.
5. Type the name of the managed property you want to create.
6. Remember to run a full crawl before this step.
7. Click Add Mapping to add a mapping to the list.
8. The crawled property selection dialog box appears, Select 'All categories', type few characters of your property, click on Find. Select the crawled property to be mapped to the managed property.
9. Click OK.
10. Click on 'Use in scopes' if you want this managed property to be used in defining scopes.
11. Click on 'OK'

This metadata property can now be used in the search refinements web part to refine your search.

Add search refiners to your search

First create your custom managed properties from central admin. I created two - Division & Department.


1.Put the page in edit mode.
2.Choose the drop down menu for “Refinement Panel” and choose Edit Web Part.
3.Open the Refinement section and uncheck the “Use Default Configuration” checkbox. Then edit “Filter Category Definition” xml as follows.
4. Collapse inner tags
5. Under filtercategories, below the second section, add the below line
category Title="Division" Description="Refinement of the Division" Type="Microsoft.Office.Server.Search.WebControls.ManagedPropertyFilterGenerator" MetadataThreshold="1" NumberOfFiltersToDisplay="4" MaxNumberOfFilters="20" ShowMoreLink="True" MappedProperty="Division" MoreLinkText="show more" LessLinkText="show fewer"
6. Make sure the metadatathreshold value is not greater than your search result.
7. Similary add tags for all your managed properties.
8. If you want to add count, add ShowCounts="Count" at the end of the tag.

Thursday, August 18, 2011

Access denied even after SPSecurity.RunWithElevatedPrivileges(delegate()

There is a problem I faced and I spent quite some time trying to solve it. I had a basic query. In my code, I was trying to access the groups as below:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
_site.AllowUnsafeUpdates = true;
Web.AllowUnsafeUpdates = true;
foreach (SPGroup group in Web.Groups)
{
if (group.Name == "WFM")
{
SPUserCollection userCollection = group.Users;
foreach (SPUser users in userCollection)
{
fuv = new SPFieldUserValue(Web, users.ID, users.LoginName);
UserValue.Add(fuv);
}
}
}

Web.AllowUnsafeUpdates = false;
_site.AllowUnsafeUpdates = false ;
});


But, it was always giving me access denied error. If we are doing Runwithelevatedprivilidges, I should not get access denied error.

Trick was to take care of two things:
1. you need to initiate a SPSite object with in the SPSecurity.RunWithElevetedPrivilges.

2. Create SPSite using ID. This is most important thing.

It worked after changing the code to below:


SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb webContext = SPContext.Current.Web;
Guid newSite;

using (SPSite site = new SPSite(webContext.Site.ID))
{
using (SPWeb web = site.OpenWeb(webContext.ID))
{
....

Phew......

Monday, June 20, 2011

Hide recycle bin and content links from Left Nav in Shp 2010

In order to hide the recyle bin and view all site content links from the left nav in sharepoint 2010 follow the below procedure. You need to retain the left nav and all other links.

Find the master page v4.master or your custom master page and find head, above head add the following code:

STYLE
.s4-specialNavLinkList
{
display:none !important;
}
/STYLE

The two links will be hidden

Thursday, March 17, 2011

Deploy a web part using feature

Create a normal cs file for the web part. To deploy it, create 2 files in the feature folder - Feature.xml, elements.xml and webpart.webpart

wpchat.webpart---------------------------------------

webParts
webPart xmlns="http://schemas.microsoft.com/WebPart/v3"
metaData
type name="Eversheds.InstantMessaging.WebParts.WPChat, Eversheds.InstantMessaging, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b0c7276684f0a390 "/type
importErrorMessageCan not import WPChat/importErrorMessage
/metaData
data
properties
property name="Title" type="string"IT Service Desk/property
property name="Description" type="string"A web part which allows the user to chat /property
property name="MissingAssembly" type="string"Cannot import this Web Part./property
property name="DisplayName" type="string"IT Service Deskproperty
property name="Name" type="string"ITServiceDesk/property
property name="AllowHide" type="bool"False/property
property name="AllowClose" type="bool"False/property
property name="ChromeType" type="chrometype"None/property
property name="AllowZoneChange" type="bool"True/property
property name="Hidden" type="bool"False/property
property name="AllowMinimize" type="bool"False/property
property name="AllowEdit" type="bool"True/property
/properties
/data
/webPart
/webParts

Feature.xml ---------------

?xml version="1.0" encoding="utf-8"?
Feature Id="D34D00B1-EC5C-4da8-8C50-009CB4B8BB42"
Title="IMCHATFEATURE"
Description="This is the chat Webpart"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Site"
ReceiverAssembly="Eversheds.InstantMessaging, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b0c7276684f0a390 "
ReceiverClass="Eversheds.InstantMessaging.FeatureReceiver.ReceiverChatFeature"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
ElementManifests
ElementManifest Location="elements.xml"/
ElementFile Location="WPChat.webpart" /
/ElementManifests
/Feature


Element.xml--------------------

?xml version="1.0" encoding="utf-8" ?
Elements xmlns="http://schemas.microsoft.com/sharepoint/"
Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE" List="113"
File Url="WPChat.webpart" Type="GhostableInLibrary"
/File
/Module
/Elements

Deploy this using wsp and the web part can be added to the web part gallery.

Tuesday, March 1, 2011

Create custom page layout using Feature

If you want to create a custom page layout using features in your solution, use the below steps-

Create a folder structure as -

12----
Template---
Features------
ABCPageLayout---
layout.aspx
Feature.xml
Elements.xml

layout.aspx-----------------------------

First create the entire layout in sharepoint designer and copy paste into .net aspx file.


Feature.xml-----------------------------

Feature Id="8C463F26-AE3C-4309-A284-AC095EB351A9"
Title="ABCPageLayout"
Description="page layoutt for use"
Version="12.0.0.0"
Scope="Site"
Hidden="True"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
ElementManifests
ElementManifest Location="ProvisionedFiles.xml"/
/ElementManifests
/Feature

Elements.xml---------------------------------

Module Name="OSGPageLayouts" Url="_catalogs/masterpage" Path="PageLayout" RootWebOnly="TRUE"
File Url="ABCPageLayout.aspx" Type="GhostableInLibrary"
Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" /
Property Name="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D;#"/
/File


Deploy this as a feature and your page layout will automatically go to the sharpoint pagelayouts.

Feature to create page of a page layout type

In case you want to create a page of a certain page layout in your solution folder, Follow the below steps.

First create the page layout in your solution using feature. (Steps in another post)

Create a structure as below in your solution -

12 --
Template --
Features ---
Page123-----
Elements.xml
Feature.xml
TemplatePage.aspx


Elements.xml------------------------

Elements xmlns="http://schemas.microsoft.com/sharepoint/"
Module Name="PublishingHomePage" Url="$Resources:cmscore,List_Pages_UrlName;" Path=""
File Url="TemplatePage.aspx" Name="Welcome.aspx" Path="TemplatePage.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE"
Property Name="Title" Value="Online Services" /
Property Name="ContentType" Value="Article Page" /
Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/HomePageLayout.aspx, /sites/onlineservices/_catalogs/masterpage/HomePageLayout.aspx" /
/File
/Module
/Elements

Feature.xml----------------------------------

Feature Id="523E7D1F-052A-42e6-BD85-3ACFC82831EE"
Title="Page123"
Description="Home Page"
Version="1.0.0.0"
Scope="Site"
Hidden="True"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
ElementManifests
ElementManifest Location="Elements.xml"/
ElementFile Location="TemplatePage.aspx" /
/ElementManifests
/Feature

change the Feature id in your code.

TemplagePage.aspx--------------------- It is the normal templatepage in every site.

%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage, Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c" %
%@ Reference VirtualPath="~TemplatePageUrl" %
%@ Reference VirtualPath="~masterurl/custom.master" %

Deploy this as a feature and your page is ready.

Window.Unload() not working in IE6

I was not able to get this simple thing working in IE 6. It worked on IE7 and IE8 and not on IE6. I found the below workaround.


SCRIPT FOR="window" EVENT="onunload"
alert("on unload");
/SCRIPT

This works without any problem.

Wednesday, January 19, 2011

Programmatically hide pages from Navigation

Suppose you are creating a page using Features, now you want that page to be hidden from the navigation when you create it. How will you go about it---

Create a Feature Receiver for that page and enter the below code at Feature activated event.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
PublishingWeb publishingWeb = null;
var objWeb = properties.Feature.Parent as SPWeb;
try
{
if (PublishingWeb.IsPublishingWeb(objWeb))
{
publishingWeb = PublishingWeb.GetPublishingWeb(objWeb);
// get the pages collection
PublishingPageCollection pages = publishingWeb.GetPublishingPages();

if (pages[publishingWeb.Url + "/Pages/chat.aspx"] != null)
{
//page exists hide it
PublishingPage page = pages[publishingWeb.Url + "/Pages/chat.aspx"];
page.IncludeInCurrentNavigation = false;
page.IncludeInGlobalNavigation = false;
}
}
}
catch (Exception ex)
{
using (StreamWriter w = new StreamWriter("C:\\log.txt"))
{
w.WriteLine(ex.ToString());
}
}
}

Wednesday, January 12, 2011

How to add a page in pages directory of Sharepoint site

Suppose you want to add a new page in the pages library of your sharepoint site programatically, how will you go about it.

Create the structure in your project -

12 - template - features - NewPageFeature -

In that create an aspx page, a feature.xml file and an elements.xml file

You will also need a cs file outside of the 12 folder, and will provide the path to the same in your aspx file.

Feature.xml ---------------------------------------------------------


Feature Id="94D273A9-B9AE-46f0-A56A-03289ACC87FF"
Title="IMCHATPAGE"
Description="Activate Chat Page"
Scope="Web"
Hidden="True"
xmlns="http://schemas.microsoft.com/sharepoint/"
ElementManifests
ElementManifest Location="Elements.xml"/
ElementManifests
/Feature


Element.xml -----------------------------------------------------------

?xml version="1.0" encoding="utf-8"? Module Name="Pages" Url="$Resources:cmscore,List_Pages_UrlName;" Path="" RootWebOnly="False"
File Name="Chat.aspx" Url="Chat.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="False" /
/Module
/Elements


.aspx file ---------------------------------------------------------

In your aspx file, include the path of you cs file like below -

Inherits="Eversheds.InstantMessaging.Chat, Eversheds.InstantMessaging,Version=1.0.0.0,Culture=neutral, PublicKeyToken=b0c7276684f0a390"

Run wsp builder and your page will be created in the pages directory of your site