Saturday, April 21, 2012

How to Add Property Restriction to Advance Search?

The property you want to add must be a crawled property and is mapped to a managed property. 

Steps to add property to crawled properties.

Open Central Administration site.
Go to Application Management. 
Under Service Applications click on Manage Service Applications
Open your Search Service Application.
Click on Content Sources under Crawling in quick launch as shown below. And click "Start Full Crawl".

After crawling completes. We need to map the property to a new managed property.

Steps to Create Managed Property and Add Mapping to crawled Property

Under Search service application management screen click Metadata Properties under Queries And Results in Quick launch. As shown below. 

Click on "New Managed Property".

On New screen give a Property Name
Select Type of Property.
Click Add Mapping. On add mapping screen search for your crawled property, select it and click Ok.
Check "Allow this Property to be used in scopes"

Click Ok on New Managed Property screen.
Re-run full crawler.

Note: Re-run the full crawler so that new managed property can be used for search in property restrictions.

Steps to Add Newly created Managed Property to Property restrictions.

Open Advance search page.
Click Edit Page Under Site Settings.
Click Edit webpart (Advance Search box webpart)
Expand Properties under webpart properties as shown below.

Click on square button after Property text box as shown below.

Copy contents. Open any xml editor tool and paste that content in editor.
Find ending tag </PropertyDefs>. Add new <PropertyDef> before this as shown below.

Now Add <PropertyRef> before all </ResultType> as shown below.

Copy All contents and replace Properties with this new xml.
Click ok on Webpart properties.
Save and Check in your page.
Check your Property restriction dropdown and your new property will be available to be used in advance search

Friday, April 6, 2012

Send Email in Sandbox Solution

In sandbox solution SPUtility.SendMail method has been blocked explicitly. So there is no way to send email using code in Sandbox solution.

I googled alot but not able to find any solution. Then I decided to try something and it worked :)

  • What I did was i created Custom Content Type using Visual Studio. 
  • Then created a Send Email Workflow Activity on my custom Content Type using Sharepoint Designer.
  • I saved that workflow as template and I exported that workflow to local Drive.
  • And then created a new Project using template "Import SharePoint Solution Package".
  • Then created another list programmatically using my custom content type.
  • Then associated my workflow with my custom list.
  • And then when ever I require to send an email. I just add records in my list. Workflow triggers and sends an email.

Steps in detail are as below:
  1. Create Empty SharePoint Project in Visual Studio. Create custom content type "EmailHelperContentType" with following custom FieldsEmailTo (single line)EmailCC (Single Line)EmailBodyText (MultiLine)EmailSubjectText (Single Line)
  2. Deploy this project.
  3. Open SharePoint Designer and open sharePoint Root web.Note: Always create Reusable Workflow at top level site.
  4. Click Reusable Workflow.
  5. On click will open up another window. Give Name and in Content type select your content type deployed in step 1.
  6. Under Actions > Core Actions > Send an Email
  7. Click "these users"
  8. For To field value click lookup > double click "Workflow Lookup for a user"
  9. Select Datasource = "Current Item"Field from source = "EmailTo"

  10. Similarly define CC, Subject and body as shown below.
  11. Click Save.
  12. Click workflow under Navigation. Select your Resuable Workflow. And in Ribbon click "Save as Template". The template will be saved in Site Assets". Refresh if you can't see it there.
  13. Select file and in Ribbon click "Export File". And save this file to desktop.
  14. Now Open Visual Studio and create new Project using template "Import SharePoint Solution Package"
  15. On next screen enter your site url and deploy as Sandbox as shown below. Click Next.
  16. Select the template created in step 12. And click Next.
  17. On next screen two items will shown up select both and click finish.
  18. You will notice one feature create for you.
  19. Add new Event receiver to feature
  20. In feature Activated Create new list " EmailHelper" and add Workflow association with list "EmailHelper" as show below.
string contentTypeName = "EmailHelperContentType";
            string workflowName = "Email Sender";
            string workflowTemplateName = "EmailHelperWorkflow";
            string listTaskName = "Tasks";
            string listHistoryName = "WorkflowHistory";
            string listEmailName = "EmailHelper";
            SPContentType siteContentType = web.Site.RootWeb.ContentTypes[contentTypeName];
            SPList historylist = web.Lists.TryGetList(listHistoryName);
            if (historylist == null)
                web.Lists.Add(listHistoryName, listHistoryName, SPListTemplateType.WorkflowHistory);
                historylist = web.Lists.TryGetList(listHistoryName);
            SPList tasklist = web.Lists.TryGetList(listTaskName);
            if (tasklist == null)
                web.Lists.Add(listTaskName, listTaskName, SPListTemplateType.Tasks);
                tasklist = web.Lists.TryGetList(listTaskName);
            SPList mainlist = web.Lists.TryGetList(listEmailName);
            if (mainlist == null)
                web.Lists.Add(listEmailName, listEmailName, SPListTemplateType.GenericList);
                mainlist = web.Lists.TryGetList(listEmailName);
                mainlist.Hidden = true;
                mainlist.ContentTypesEnabled = true;
                SPWorkflowTemplate workflowTemplate = null;
                foreach (SPWorkflowTemplate template in web.WorkflowTemplates)
                    workflowTemplate = template;
                    // We'll take a template everyone has.
                    if (workflowTemplate.Name == workflowTemplateName) break;
                SPWorkflowAssociation association =
                association.AllowManual = true;
                association.AutoStartCreate = true;
                association.AutoStartChange = false;

For more detail on Sandbox Solution Visit Here

Popular Posts