Tuesday, November 4, 2008

SharePoint Web Services and Java

Scenario:
You want to talk to MOSS using Java.. No problem...

Solution:
SharePoint Web Services

Article:
http://www.infoq.com/articles/swanson-moss-web-services

Sunday, November 2, 2008

Adding a new metadata property to search

Scenario:
You want to add new Managed Properties to the SharePoint Search results.

Solution:
Shared Service Administration Web Application provide you the options to add new Managed Properties

Steps:
1. Navigate to Central Administrative Web Application
2. Click on 'SharedServices1' link from the left navigation
3. Click on 'Search Settings'
4. Click on 'Metadata property mappings'
5. Click on 'New Managed Property'
6. Type a Name for the property and select the Type
7. Click on the 'Add Mapping' button from the bottom section
8. Search for the property or the column name , you want to map.
Note: If you are not able to find the column name or property you want to map, full crawl might be required for SharePoint Search to pickup the proprty.
9. Click 'OK' Button to add it
10. Run a full crawl for the Content Source containing the column infomation

Article:
http://sharepointmagazine.net/technical/customisation/customizing-search-series-creating-new-managed-properties-from-bdc-crawled-content

Add FBA users to sharepoint programmatically

Scenario:
You want to add FBA users to one of the SharePoint Group

Solution:
1) Create FBA Membership User
2) Create SharePoint User
3) Get your site instance
4) Add SharePoint User to a Desired group

Code:


MembershipUser membershipUser = Membership.CreateUser(login, password, email);

SPWeb spWeb = SPControl.GetContextWeb(HttpContext.Current);

SPUser spUser = spWeb.EnsureUser(login);
SPGroup spGroup = spWeb.SiteGroups["VISITORS"];
spGroup.AddUser(spUser);

Possible Errors:
[SPException: The user does not exist or is not unique.]
or 
When You try to put this new user into a sp group, the web is redirected to the login page


Article:
http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959/

List Template Id's and Base Type

ListTemplateIds for various list types is as following:


100 Generic list
101 Document library
102 Survey
103 Links list
104 Announcements list
105 Contacts list
106 Events list
107 Tasks list
108 Discussion board
109 Picture library
110 Data sources
111 Site template gallery
112 User Information list
113 Web Part gallery
114 List template gallery
115 XML Form library
116 Master pages gallery
117 No-Code Workflows
118 Custom Workflow Process
119 Wiki Page library
120 Custom grid for a list
130 Data Connection library
140 Workflow History
150 Gantt Tasks list
200 Meeting Series list
201 Meeting Agenda list
202 Meeting Attendees list
204 Meeting Decisions list
207 Meeting Objectives list
210 Meeting text box
211 Meeting Things To Bring list
212 Meeting Workspace Pages list
301 Blog Posts list
302 Blog Comments list
303 Blog Categories list
1100 Issue tracking
1200 Administrator tasks list
Base Type values is as following:

0 Lists
1 Document libraries
3 Discussion boards. Does not support custom view styles.
4 Surveys. Does not support custom views or view styles.

Batch update list items using CAML

Scenario:
You want to batch update small amount of items in a list ( usually less then 100 in one time items ) 

Solution:
You can use CAML query to make the BULK update. Performace will degrade as the number of items to be updated increases. This example updates 100 items each time.

Code:

StringBuilder methodBuilder = new StringBuilder();
string batch = string.Empty;
DateTime currentDate = DateTime.Now;
string formattedDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(currentDate);

string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">{0}</ows:Batch>";

string methodFormat = "<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"ID\">{2}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Status\">{3}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Status_x0020_Date\">{4}</SetVar>" +
"</Method>";

using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{

// Get the list containing the items to update.
SPList list = web.Lists["Shared Documents"];
string listGuid = list.ID.ToString();

// Query to get the unprocessed items.
SPQuery query = new SPQuery();
query.Query = "<Where><Neq><FieldRef Name='Status'/>
<Value Type='Choice'>Open</Value></Neq></Where>";
query.ViewAttributes = "Scope='Recursive'";
query.RowLimit = 100;

SPListItemCollection unprocessedItems = list.GetItems(query);

do
{
SPListItemCollection unprocessedItems = list.GetItems(query);

//Process all the returned items in this page
// Build the CAML update commands.
for (int i = 0; i < unprocessedItems.Count; i++)
{
int itemID = unprocessedItems[i].ID;
methodBuilder.AppendFormat(methodFormat, itemID, listGuid, itemID, "In-Progess", formattedDate);
}

// Put the pieces together.
batch = string.Format(batchFormat, methodBuilder.ToString());

// Process the batch of commands.
string batchReturn = web.ProcessBatchData(batch);

query.ListItemCollectionPosition = unprocessedItems.ListItemCollectionPosition;
} while (query.ListItemCollectionPosition != null);
}
}
}
Article:http://msdn.microsoft.com/en-us/library/cc404818.aspx

Friday, October 24, 2008

SharePoint developer Toolbox ( Google to get latest downloads )

Links:
CAMLViewer
CAML Viewer

CAML Builder
http://weblogs.asp.net/jan/archive/2008/05/28/new-version-of-the-caml-query-builder-tool.aspx

Internet Developer ToolBar
http://www.microsoft.com/downloads/details.aspx?familyid=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en

FireBug
https://addons.mozilla.org/en-US/firefox/addon/1843

Google Chrome( comes with document inspector )
Chrome

WSPBuilder
http://www.codeplex.com/wspbuilder

FAR
http://www.rarlab.com/

BDCMetaMan
http://www.lightningtools.com/default.aspx

Application Pool Recycle and Warmup
http://www.harbar.net/articles/APM.aspx

SPDisposeCheck
http://code.msdn.microsoft.com/SPDisposeCheck

Some more...
http://blog.thekid.me.uk/archive/2008/09/29/my-current-sharepoint-development-toolkit.aspx

http://www.sharepointdevwiki.com/display/public/Other+SharePoint+Development+Tools

http://blogs.tamtam.nl/mart/SharePointToolsGaloreV3.aspx

System Essential Tools

Screen Capture
Mercer Tool

Search Files
Grep for Windows

Programatically searching sharepoint

Scenario:
You want to write custom code to return the results of a keyword query

public ResultTableCollection Search(string keyword) {

KeywordQuery query = new KeywordQuery(ServerContext.Current);
query.QueryText = keyword;
return query.Execute();
}
This code creates an instance of the KeywordQuery class. This class allows you to search SharePoint for existing keywords. You simply set the QueryText property to a keyword query and call the Execute method.

If you want to use a SQL query, you must use the FullTextSqlQuery class.
using (SPSite site = new SPSite("http://localhost")){

using( SPWeb web = site.OpenWeb()){

FullTextSqlQuery query = new FullTextSqlQuery(site);

query.QueryText =

"Select Title, Rank, Path from Scope() where freetext('Search Keyword') AND site='http://moss2007' ORDER BY Rank desc";

query.RowLimit = 100;
query.ResultTypes = ResultType.RelevantResults;

ResultTableCollection results = query.Execute();
ResultTable result = results[ResultType.RelevantResults];

while (result.Read()){
Console.WriteLine(result[0].ToString() + ", " + result[1].ToString() + ", " + result[2].ToString());
}
}
}