Posted by zieglers on October 9, 2009

Here is a little code snippet re custom metadata search using SharePoint Object Model. This code is just querying a predefined scope – MySearchScope - for custom metadata field – MyCustomMetadata. Search Criteria is passed from a text field named txtSearchCriteria.
There are couple of things to note here:
1. Make sure you created a Managed Property for your custom metadata in your SSP.
2. Make sure that your custom scope is configured properly. It’s good practice to have a separate content source assigned for your custom scope.
3. Make sure that your custom scope is Shared, compiled and ready to use.
4.Be careful with QueryText construction. Custom scope must be enclosed in ” ” in WHERE clause.
==================================================================================
DataTable queryDataTable = new DataTable();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
// Construct query object based on document number entered bu user
// Create a new FullTextSqlQuery class – use property intializers to set query
FullTextSqlQuery myQuery = new FullTextSqlQuery(ServerContext.Current)
{
// Construct query text
QueryText = “SELECT Title, Rank, Size, Description, Write, Path, MyCustomMetadata FROM portal..scope() WHERE MyCustomMetadata = ‘” + txtSearchCriteria.Text + “‘ AND ( (\“SCOPE\“ = ‘MySearchScope ‘) ) ORDER BY \”Rank\” DESC”,
ResultTypes = ResultType.RelevantResults
};
// execute the query and load the results into a datatable
ResultTableCollection queryResults = myQuery.Execute();
ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];
// Load table with results
queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);
}
});
==================================================================================
zieglers
Posted in IT Stuff, MOSS & WSS 3.0 | Tagged: Custom Metadata, FullTextSqlQuery, Managed Property, metadata, scope, search metadata, Search Object Model, sharepoint | Leave a Comment »
Posted by zieglers on October 6, 2009

Sometimes you might wanna assign item level permissions to a list item instead of using Elevated Priviliges block throughout the whole code. This might happen in case you need to temporarily give a user higher priviliges than she has.
Typical example would be to provide a site reader exceptional functionality on a specific list item. In this case you break role inheritance and give the permissions you want. Here is a little snippet for that:
======================================================================
RunWithElevatedPriv
…using SPSite…
…using SPWeb…
// Get current user obj
SPUser user = SPContext.Current.Web.CurrentUser;
// Get current user token
SPUserToken token = user.UserToken;
// Get current user principal
SPPrincipal principal = (SPPrincipal)SPContext.Current.Web.CurrentUser;
// Get current user role assignment
SPRoleAssignment RoleAssignment = new SPRoleAssignment(principal);
// Get Your Custom Role definition
int SiteContributorId = -1;
SPRoleDefinition RoleDefSiteContributor = new SPRoleDefinition();
foreach (SPRoleDefinition roleDef in web.RoleDefinitions)
// Find Your Custom Role Definition id
if (roleDef.Name == “Your Custom Role Name”)
SiteContributorId = roleDef.Id;
if (SiteContributorId != -1) {
RoleDefSiteContributor = web.RoleDefinitions.GetById(SiteContributorId);
// Add Your Custom Role to role assignments
RoleAssignment.RoleDefinitionBindings.Add(RoleDefSiteContributor);
}
if (!impersonatedListItem.HasUniqueRoleAssignments)
// Break role inheritance
impersonatedListItem.BreakRoleInheritance(true);
// Add Your Custom Role assignment to list item
impersonatedListItem.RoleAssignments.Add(RoleAssignment);
// Update changes
impersonatedListItem.Update();
…close using SPWeb…
…close using SPSite…
======================================================================
zieglers
Posted in IT Stuff, MOSS & WSS 3.0 | Tagged: BreakRoleInheritance, Impersonate, item level security, RoleDefinitions, SPRoleAssignment, SPRoleDefinition | Leave a Comment »