Thursday, July 28, 2011

Open a utf8 encoded file saved in windows on unix or linux

Scenario:
Programmatic creation of xml files using c# in a sharepoint library. These files are then manually sent over to a linux system that reads these files using bash od command. The output of the linux command sees 3 special characters in front of the xml declaration tag which is not visible when we open the file in notepad on windows.

Solution:
The 3 special characters are displayed due to BOM (Byte order marker) present as the first character in the generated xml file which is not visible in notepad. The solution is to remove the BOM character programmatically before writing the file to disk / sharepoint.

This can be easily achieved using,

Pass false as a parameter to the constructor so that it ignores the BOM while encoding files.


Wednesday, July 27, 2011

SP2010 Tag external sites.


Click on Tag this page on an internal site and read the information on the dialog box.

Tuesday, July 26, 2011

SharePoint Most Popular / Viewed Documents webpart

Ideas to build a SharePoint Most Popular Documents webpart

1. IIS Log
a. we use a logparser to push iis logs to db monthly for reporting. tweak the code to push only document read info to a db table daily.
b. build a webpart to read the sql table.
2. Audit logs (bad idea)
a. turn on site collection audit logs to track document view.
b. use audit log object model to read the info you need.
3. Site collection usage reports -> i dont know if the reports store the document information. I dont think it does, maybe worth a check.
4. Complicated approach, but could be extended,
a. Deploy a delegate control at site(any level?) level with some javascript, which will track document clicks (trust me this will be complicated) and uses ajax to store it back the server in a config list.(It could just write to a links list).
b. Use the list view webpart / custom webpart to display the list view.

Saturday, July 16, 2011

Read sharepoint xml file into memory dataset

using (MemoryStream mStr = new MemoryStream(folder.Files[0].OpenBinary()))
{
DataSetVariable.ReadXml(mStr);
}

Friday, July 15, 2011

Create SPFolder and upload SPFile programmatically

SPList list = web.GetListFromUrl(u);
SPFolder folder = web.GetFolder(string.Format("0}/{1}",list.RootFolder.Url,foldername));
if (!folder.Exists)
{
folder = list.RootFolder.SubFolders.Add(foldername);
}

using (MemoryStream stream = new MemoryStream())
{
nds.WriteXml(stream);
SPFile file = folder.Files.Add(filename, stream);
}

Thursday, July 14, 2011

Add user to email distribution list

Email distribution lists can be easily manipulated using ADSI code (System.DirectoryServices) and LDAP protocol.