Sharepoint List Attachments in a Webpart : The Solution

It's not a headache until you can do without it, but to use the sharepoint's attachment controls can be a pain in you know what. Try the solution below and save your self from same.

~Adding attachment Functionality in a Web Part.

//Attachment Controls
//Add the Attachment Controls in the webpart, the attachment upload is the //control to upload and AttachmentsField displays the list of all attachments
AttachmentButton btnAttach;
AttachmentUpload uploadAttach;
AttachmentsField fieldAttach;
HiddenField hDeleteAttachs;
protected override void RenderWebPart(HtmlTextWriter output)
//Add the javascript which will handle the list of attachments that have been //marked as deleted and add them to the Hidden Fiels, Actually its adds the //GUID of the Attachment(the delete attachment function use that)

output.Write("function RemoveAttachmentFromServer(guid, bRecycleBinEnabled){");
output.Write("var L_ConfirmDelete_TXT='Are you sure you want to delete this attachment?';");
output.Write("var L_ConfirmRecycle_TXT='Are you sure you want to send this attachment to the site Recycle Bin?';");
output.Write("var strWarning;document.getElementById('"+hDeleteAttachs.ClientID+"').value += guid + ';';");
output.Write("if (bRecycleBinEnabled){");
output.Write(" strWarning=L_ConfirmRecycle_TXT; } else { strWarning=L_ConfirmDelete_TXT; }");
output.Write("if (confirm(strWarning)) {");
output.Write("if (document.getElementById('idAttachmentsTable').rows.length==0){");
output.Write("document.getElementById('idAttachmentsRow').style.display='none';} }} fuction gotoURL(sURL){window.location=sURL; return false;}");

//Render all the controls
//Looks simple aah! But remember to include the attachment button in a span //with id “part1”
// Yes, that is required, otherwise keep expermenting…..
//The Attachment Upload control has to be in a control with ID = “idAttachmentRow”
//Replace '<' with <
//P.S. : This is one of the most important aspect

output.Write("’<’span id='part1'’>’");
output.Write("’<’/td’>’<’/tr’>’<’tr’>’<’td id='idAttachmentsRow'’>’");

//Create Controls

protected override void CreateChildControls()
//Attachment Controls
btnAttach = new AttachmentButton();
btnAttach.ListId = list.ID;
btnAttach.ControlMode = SPControlMode.New;
btnAttach.Text = "Add Attachment";
fieldAttach = new AttachmentsField();
fieldAttach.ListId = list.ID;
fieldAttach.FieldName = "Attachments";
fieldAttach.ControlMode = SPControlMode.New;
uploadAttach = new AttachmentUpload();
uploadAttach.ListId = list.ID;
uploadAttach.ControlMode = SPControlMode.New;
//Hidden Field as mentioned above
hDeleteAttachs = new HiddenField();
hDeleteAttachs.ID = "hHiddenFields";
//Submit ,call attachment functions, First Add then delete
void btnSubmit_Click(object sender, EventArgs e)
//That's the way it is
AddAttachments(ref item);
DeleteAttachments(ref list, ref item);
//Delete the Attachments, Function
void DeleteAttachments(ref SPList list, ref SPListItem item)
#region Delete
SPFileCollection files = null;
SPFolder itemFolder = null;
//Get tha Attachment Folder
string strAttchmentFolderURL = item.Attachments.UrlPrefix + "/" + item.ID.ToString();
if (list.RootFolder.SubFolders.Count > 0)
if (list.RootFolder.SubFolders["Attachments"] != null)
itemFolder = list.RootFolder.SubFolders["Attachments"];
//Read the hidden field contaning the list of deleted attchments GUID
if (!hDeleteAttachs.Value.Equals(""))
string strValue = hDeleteAttachs.Value;
string[] strIDs = strValue.Split(';');
for (int i = 0; i < strID.Length; i++)
string strTempID = strIDs[i];
//I know there is better way to remove characters
strTempID = strTempID.Replace("{", "");
strTempID = strTempID.Replace("}", "");
//Get all the files in Attachment Folder that matches the GUID
#region getFiles
if (itemFolder.SubFolders[strAttchmentFolderURL] != null)
files = itemFolder.SubFolders[strAttchmentFolderURL].Files;
foreach (SPFile file in files)
if (strTempID.ToLower().Equals(file.UniqueId.ToString().ToLower()))
//Delete the file finally
//Add attchments
void AddAttachments(ref SPListItem item)
for (int i = 0; i < (Page.Request.Files.Count - 1); i++)
//Get the list of files for attachments
HttpPostedFile newAttach = Page.Request.Files[i];
byte[] fileContents = new byte[newAttach.ContentLength - 1];
newAttach.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
newAttach.InputStream.Read(fileContents, 0, newAttach.ContentLength - 1);
System.IO.FileInfo fInfo = new System.IO.FileInfo(newAttach.FileName);
item.Attachments.Add(fInfo.Name, fileContents);
catch { continue; }
catch {
That’s all folks !

Microsoft's (SAAS) Software-As-A-Service business

Stephen Elop, president of Microsoft's $18.9 billion Business Division, this week reiterated a bold prediction about the future of Microsoft's software-as-a-service business. Microsoft foresees half of Exchange, SharePoint, and Dynamics CRM revenue coming from online versions of those products within five years.

Elop was in New York meeting with analysts and customers, following Microsoft's announcement three weeks ago of general availability of Exchange Online and SharePoint Online. Microsoft's Office Communications Online service goes into testing early next year. Its Dynamics CRM Online has been available for several months.

If you put Elop's prediction of 50% of Exchange/SharePoint/CRM revenue coming from online services into the context of Microsoft's current business, Microsoft's cloud should become a billion-dollar operation in short order. SharePoint alone is a $1 billion business today, growing at 35% annually. At that growth rate, SharePoint in the cloud would be a $1 billion business within three years, if Elop's forecast holds true. Add in revenue from Exchange, Dynamics CRM, Office Communications, and other online services and Microsoft gets to the $1 billion cloud revenue mark within, say, 24 months.

Whether Microsoft can deliver on its ambitious cloud revenue projection remains to be seen, but it's not the first time I've heard the "50% in five years" forecast. Senior VP Chris Caposella made the same projection earlier this year, so company management is on the same page. Given the sheer size of Microsoft's application business -- the Business Division is expected to grow to $20 billion this fiscal year -- even modest success will translate into a rainfall of cloud revenue.

Thanks  John Foley for the text.

Visual Studio 2010 likes SharePoint

Jason Zander, General Manager for Visual Studio, recently announced Visual Studio 2010 Tools for Sharepoint.

Items addressed in Zander's TechEd EMEA keynote:

Building and Debugging
Visual Studio will be able to building and debug SharePoint projects. "F5 Just Works!"

Server Explorer Integration
SharePoint Connections will be an option in the VS Server Explorer. Standard Sharepoint artifacts will be viewable: ContentTypes, Features, Templates, Lists, Sites, Workflows, Workspaces. Direct manipulation of some artifact attributes will be supported through VS property grid integration.

Windows SharePoint Services Project (WSP) Import
This will automate the manual task of creating Windows SharePoint Services solution package files. Previously, to create a solution package file, a developer had to use the Makecab.exe console application that is included in the Microsoft Cabinet Software Development Kit (SDK). Makecab.exe requires specifying the Diamond Directive File (.ddf) that contains a list of all the files to include in the package. Much of this will be automated.

Visual Web Part Designer
A new WSYWIG designer will exist for authoring Web Parts. The designer will also load a user control as a web part for SharePoint. This seems to be a more tightly integrated version of the widely-used SmartPart, which is a Web Part that allows hosting of ASP.NET User Controls in SharePoint.

Event Receiver Wizard
Adding Event Receivers and connecting them to Sources can be done visually through a wizard.

Workflow Integration
A new ASPX Workflow Initiation form for Workflow Project will be added. Workflow initiation forms will have a visual designer.

Packaging Editor
A new Packaging Explorer will exist that supports editing Packaging and structuring the SharePoint Features and WSP file.

Thanks Al Tenhundfeld for bringing this to us.

Display Username for the Logged on user on a page through a DataView

It is been a long time that I had penned down on Sharepoint Tip, One of our prestigious Client wanted me to display Current Logged on User Name, So here we go on Some Useful TIPS, The restriction was not to use the SharePoint Control i.e '<'wssuc:Welcome id="IdWelcome" runat="server" EnableViewState="false"'>' ''. But we can't write server-side code in SharePoint ASPX pages. The way I get around this limitation is to code XSLT and make it do what I want.

1. Insert a Data View Web Part on the Page. From any of the Lists drop a Title column on that dataview.

2. Click on Data > Conditional Formatting

3. Select any data value and Click on Create

4. Click on Show Content under conditional formatting.

5. Create a Condition that says: Title EQUALS [Current User] and click OK back to the Design view

6. Type “Welcome to this site“ inside one of the Table cells

7. Switch to Code view

8. Find the following code:

'<'ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"'/>'

9. Change this code to this:

'<'ParameterBinding Name="UserID" Location="CAMLVariable;ServerVariable(LOGON_USER)" DefaultValue="CurrentUserName"'/>'

10. Scroll up in the code and find “Welcome to this site”

11. Change code in front of your Welcome Message to this: “'<'xsl:value-of select="$UserID"'/>'

12. Save the page.

Hope this works for you !

Sharepoint & Azure

Here is the extract from SharePoint's official blog, we hope to see more of Azure and Wss in coming days.

In the Azure Services Platform, a number of developer services were mentioned including Microsoft SQL Services, Microsoft Dynamics CRM Services & Microsoft SharePoint Services. Keep in mind that this not the same as Windows SharePoint Services 3.0 which shipped in Windows Server.  Microsoft SharePoint Services is a developer service that will be available as part of Azure in the future. (Possibly the infrastructure on same line will be available for silveright in next version of sharepoint)
The keynote also emphasized Microsoft’s investment in cloud applications that developers can extend and customize. Specifically, David Thompson (VP of Online Services) talked about Microsoft SharePoint Online  and demonstrated how developers can write code against SharePoint Online web services as well as make customizations with SharePoint Designer. For example, using the Data View Web Part to surface data from an external source – this could be a web service living in Windows Azure. In fact, later in the week at PDC, there’s a breakout session that walks through the different ways SharePoint Online can be customized.
Here are answers to a few frequently asked questions:
What is Microsoft SharePoint Services in Azure?
In the future, developers will have access to SharePoint functionality in the Azure Services Platform (“Microsoft SharePoint Services”). With the flexibility to use familiar developer tools like Visual Studio, developers will be able to rapidly build applications that utilize SharePoint capabilities as building blocks for their own applications. Developers can expect a breadth of SharePoint capabilities across the spectrum of on-premises, Online and the Azure Services Platform.
Where can I find more information about Microsoft SharePoint Services in Azure?
We have not announced any further detail or release dates. We will release more information about this in the future.
How do developers get ready for Microsoft SharePoint Services?
Keep developing on SharePoint technology (MOSS, WSS, SharePoint Online)! You can learn more about SharePoint Development here.

"Sharepoint" Search Trend

The Search trend for "Sharepoint" on google seems interesting
Europe in lead with India and US is a welcome trend. I guess that Europe as combined will be ahead of India & US.

3.United States
10.United Kingdom
1.Redmond, WA, USA
2.Bangalore, India
3.Mumbai, India
4.Singapore, Singapore
5.Washington, DC, USA
6.Seattle, WA, USA
7.Minneapolis, MN, USA
8.Houston, TX, USA
9.Atlanta, GA, USA
10.Amsterdam, Netherlands

And for "Moss 2007"

8.United States
1.Bangalore, India
2.Chennai, India
3.Mumbai, India
4.Delhi, India
5.Singapore, Singapore
6.Dallas, TX, USA
7.Washington, DC, USA
8.Houston, TX, USA
9.London, United Kingdom
10.Chicago, IL, USA