Tuesday, November 24, 2009

Sharepoint Tips n Tricks - Part 2

In continuation with earlier post -
Sharepoint Tips n Tricks

1) Check whether a given SPField exist or not before fetching value from it:


string columnValue = string.Empty;
string columnInternalName = string.Empty;
string fieldName = "Myfield";
SPListItem listItem; //Code to get SPListItem

//Check required SPField exist or not
if (listItem.ParentList.Fields.ContainsField(fieldName))
{
//Get internal name of SPField
columnInternalName = listItem.ParentList.Fields[fieldName].InternalName;

//Fetch column value based on internal name
if (listItem[columnInternalName] != null)
columnValue = listItem[columnInternalName].ToString();
}



2) SharePoint 2007 recommended guidelines for managing site collections, site, lists, and documents according to business needs:

· 50,000 site collections per content database

· 500 MB per site collection (default quota maximum)

· 50 MB file size limit (recommended) up to 2 GB (maximum)

· 2000 items per list view


3) Check whether page is in Edit Mode or not


if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
{
// your code to support display mode
}
else if(Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit)
{
// your code to support edit mode
}


OR


WebPartManager wp = WebPartManager.GetCurrentWebPartManager(Page);

if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)
btnLink.InnerText = "Edit Page";
else if (wp.DisplayMode == WebPartManager.DesignDisplayMode)
btnLink.InnerText = "Exit Edit Mode";
else
btnLink.Visible = false;



Reference...
http://www.codeproject.com/KB/sharepoint/SwitchWPMode.aspx

http://mystepstones.wordpress.com/2008/09/23/detecting-the-current-mode-displayedit/


4) SPFeature to deploy page in a SharePoint site


Feature.xml
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="f08674ee-2b0e-41aa-8d28-24e788d03c11"
Title="PageDeployment"
Description="Description for PageDeployment"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
<ElementFile Location="MyPage.aspx" />
</ElementManifests>
</Feature>


Elements.xml
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Pages" Path="" Url="">
<File Name=" MyPage.aspx" Url=" MyPage.aspx" IgnoreIfAlreadyExists="FALSE" NavBarHome="True"></File>
</Module>
</Elements>

Code to download document from SharePoint Document Library

Following code snippet demonstrate the way to download a MS Word document (or any file) from SharePoint SPDocumentLibrary-


string siteURL = "http://localhost:80/"; //Write valid site URL
//Get SPSite and SPWeb instance
using (SPSite spSite = new SPSite(sireURL))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
//Get time stamp to download file with unique name
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");

//Get folder path
string directoryPath =
Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
"MyFolder");

//Create directory if it not exist
if (!(Directory.Exists(directoryPath)))
{
Directory.CreateDirectory(directoryPath);
}

//Get template url
string documentUrl; //Write valid document URL

//Download file in file system
SPFile myFile = spWeb.GetFile(documentUrl);
byte[] binFile = myFile.OpenBinary();

//Generate file name
filename = Path.Combine(directoryPath, strFileName + ".doc");

//Do file write operation
FileStream fs =
new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(binFile);
bw.Close();
}
}

MSMQ operations in C#

Following code snippet demonstrate the way to do some common operations with Microsoft Message Queuing (MSMQ)-

1) Send message in MSMQ -


string queueName = @".\Private$\MyMSMQ";
string myMessage = "Hello World";
MessageQueue messageQueue = null;
Message oMsg = null;

//Sending Messages to MSMQ
if (MessageQueue.Exists(queueName))
{
messageQueue = new MessageQueue(queueName);

oMsg = new Message(myMessage);
messageQueue.Send(oMsg);
}
else
{
//Create the MessageQueue
messageQueue = MessageQueue.Create(queueName);
//Set Queue permission
//messageQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl,
// AccessControlEntryType.Set);
messageQueue.SetPermissions("Everyone",
MessageQueueAccessRights.PeekMessage |
MessageQueueAccessRights.ReceiveMessage |
MessageQueueAccessRights.DeleteMessage |
MessageQueueAccessRights.WriteMessage,
AccessControlEntryType.Set);

oMsg = new Message(myMessage);
messageQueue.Send(oMsg);
}



2) Receive Message from MSMQ -


string myMessage= string.Empty;
string queueName = @".\Private$\MyMSMQ";

//Receiving Messages from MSMQ
MessageQueue messageQueue = new MessageQueue(queueName);

//To keep a check that if no messages present in MSMQ,
//control should return back in 1 second.
TimeSpan timeout = new TimeSpan(1);
Message message = null;

//Get all the messages present in the MSMQ and iterate through it.
MessageEnumerator enumerator = messageQueue.GetMessageEnumerator2();

if (enumerator.MoveNext())
message = enumerator.RemoveCurrent(timeout);

if (message != null)
{
messageBody = message.Body.ToString();
}




3) Display all the messages present in the MSMQ (without removing messages from queue)


StringBuilder allMessages = new StringBuilder();
string queueName = @".\Private$\MyMSMQ";

//Enumerating Messages from MSMQ
MessageQueue oMq = new MessageQueue(queueName);

foreach (Message message in oMq)
{
stringBuilder.Append(message.Body + ";");
}



4) Deletes all the messages contained in the MSMQ queue


string queueName = @".\Private$\MyMSMQ";

//Deletes all the messages contained in the MSMQ queue
MessageQueue messageQueue = new MessageQueue(queueName);
messageQueue.Purge();



Nice link to refer -
Use Microsoft Message Queuing in C# for inter-process communication

Google