If you’re using LINQ just be careful when you are using functions or properties while comparing the data
For example if you have a date field and you want to get today items, if you use the following code
var result = from item in listofitems where item.Date == DateTime.Today select item;
This code it will take very much long time of the following code
DateTime today = DateTime.Today;
var result = from item in listofitems where item.Date == today select item;
I test the first code with around 5,000,000 items, it takes 2.684 seconds but the 2nd one It takes 0.302 of the second, so that means if you put a function or other property which is takes 1 millisecond, it means your query will take 1 second for checking 1000 item which is no need for this time, instead just save the result of the function or property before doing the query, save it in a variable and then pass it to the query, as I did in the second code.
If you create a workflow using Visual Studio for WSS 3.0 and you want to copy or move this workflow to other server like moving it from development server to the production server, so you have to follow these steps
- Create a folder with the name of the workflow in the Feature folder which is usually having this path “\\mossserver\c$\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”
- Copy the files “feature.xml” and “workflow.xml” to the new folder
- Copy the dll file for your workflow to the assembly folder which is placed in Windows folder which is “C:\Window\Assembly”
- Install the feature in your moss server by using this command line under folder "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN" run this command
“stsadm -o installfeature -filename thenewfolderName\feature.xml -force"
After that you will find the workflow in your moss server .
Sometimes you want to allow the user to customize the web part so the custom properties will help you on this with a good user friendly interface like changing the title for the web part.
The property for the web part same like any property for any class but you need to add some attribute to display it in the edit pane in the SharePoint and it will save the data in SharePoint storage as xml, so have to add some xml attributes on the web part.
First thing here is the post which is talk about Creating SharePoint Web Part.
After creating the web part class you have to add XmlRoot attribute to the class
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;
namespace MyWebPart
{
[XmlRoot(Namespace = "MyWebPart")]
public class Hello : WebPart
{
}
}
And then create the property and you have to add some attributes
· Browsable:
This attribute allow you to display the property in edit pane, its accepting a Boolean value.
· Category:
This attribute is the title of the section of properties and this title will appear as a section in the edit pane and the properties will appear under that title the default value is Miscellaneous
· DefaultValue:
This attribute allow you to add a default value for the property
· Description:
This attribute allow you to add description for the property which is appear as tool tip and the property name is default value.
· FriendlyNameAttribute:
This attribute allow setting a friendly name instead of the name in the code which is not allowing the spaces for the property and this name will appear on the edit pane. The default value is the property name
· WebPartStorage:
This attribute allow you to determine when this property appears and it accept three values. Storage.Shared: to display this property when the user is in shared view of the page. Storage.Personal: to display this property when the user is in Shared or Personal view of the page. Storage.None: to hide the property.
So your property it will be something similar like this
private string body;
[Browsable(true), Category("My Properties"), WebPartStorage(Storage.Shared), FriendlyNameAttribute("Body"), Description("Type the body for this web part")]
public string Body
{
get { return body; }
set { body = value; }
}
And then you can use this property in your code
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;
using System.ComponentModel;
namespace MyWebPart
{
[XmlRoot(Namespace = "MyWebPart")]
public class Hello : WebPart
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(Body);
base.Render(writer);
}
private string body;
[Browsable(true), Category("My Properties"), WebPartStorage(Storage.Shared), FriendlyNameAttribute("Body"), Description("Type the body for this web part")]
public string Body
{
get { return body; }
set { body = value; }
}
}
}
The web part property will be like this

If you trying to writing server side code in a page in your SharePoint write you will get the error
An error occurred during processing of {page filename}. Code-blocks are not allowed in this file.
To enable writing server side codes in your MOSS website add the following bold line in the web.config for the web application
<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
<PageParserPaths>
<PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />
</PageParserPaths>
</SafeMode> After that you will be able to write the server side code anywhere in the site pages, layoutpages or masterpages.
You also can add a custom controls “ascx” files which is allowing you to put your controls, html layout, css, javascripts and events handlers, so you can create your own control and you add whatever you want of ASP.NET controls and server side codes, but first you have to change trust level on the web.config of your application to Full. You will find trust tag in the web.config and the default level is WSS_Minimal change it to Full <trust level="Full" originUrl="" /> Then you can create the Control with or without a code behind and this is a sample control I called it MyControl.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs" Inherits="MyControl" %> <asp:Label ID="Label1" runat="server" Text="Label">asp:Label> <asp:TextBox ID="TextBox1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> The code behind in C# called MyControl.ascx.cs using System; public partial class MyControl : System.Web.UI.UserControl { protected void Button1_Click(object sender, EventArgs e) { Label1.Text = TextBox1.Text; } } After that you have to put your control files (.ascx) and the code behind if you use code behind (.cs or .vb) in the application folder which is usually under C:\Inetpub\wwwroot\wss\Virtual Directory\{Port Number} , and you can put your controls in a folder called Controls just to put the controls in one place and then load this control in your web part and add it using Microsoft.SharePoint.WebPartPages; namespace MyWebPart { public class Hello : WebPart { protected override void CreateChildControls() { Controls.Add(Page.LoadControl("/Controls/MyControl.ascx")); base.CreateChildControls(); } } } Then build your web part and deploy it like first sample.
I explained before how to create a simple SharePoint web part, but if you want to use ASP.NET controls or any of server side controls you can add it in the Controls property, which is a controls collection for the web part.
To do that you can override CreateChildControl method and there you add any server side controls
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
namespace MyWebPart
{
public class Hello : WebPart
{
private TextBox textBox1;
protected override void CreateChildControls()
{
textBox1 = new TextBox();
textBox1.ID = "TextBox1";
Controls.Add(textBox1);
base.CreateChildControls();
}
}
}
You can also add events handlers to these controls
public class Hello : WebPart
{
private TextBox textBox1;
private Button button1;
private Label label1;
protected override void CreateChildControls()
{
label1 = new Label();
Controls.Add(label1);
textBox1 = new TextBox();
Controls.Add(textBox1);
button1 = new Button();
button1.Text = "Submit";
button1.Click += new System.EventHandler(button1_Click);
Controls.Add(button1);
base.CreateChildControls();
}
void button1_Click(object sender, System.EventArgs e)
{
label1.Text = textBox1.Text;
textBox1.Text = string.Empty;
}
}

By this way you need to build it and copy the dll to the bin folder and add the wep part to your web page.
To create Web Part for MOSS 2007 you can create new Class Library Project with C# or VB and you have to add “System.Web”, “Microsoft.SharePoint.dll” to the references of the project. After that you can create class and inherit a WebPart class from Microsoft.SharePoint.WebPartPages namespace which is in the “Microsoft.SharePoint.dll”. In this Web Part you can write your code C# or VB, for this sample I will write Hello World as html text and the shortest way to do that you can override Render method in the webpart class and write you html text!! using Microsoft.SharePoint.WebPartPages; namespace MyWebPart { public class Hello : WebPart { protected override void Render(System.Web.UI.HtmlTextWriter writer) { writer.Write("<h1>Hello World</h1>"); base.Render(writer); } } } Then build your project and get dll and put it the bin folder for your SharePoint web application which is usually under C:\Inetpub\wwwroot\wss\Virtual Directory\{Port Number for your application} And under this folder you will find the web.config for your application and you need to add this control in safe controls, and to do that you need to add this line inside SafeControls tags in the web.config <SafeControl Assembly="MyWebPart" Namespace="MyWebPart" TypeName="*" Safe="True" /> To use this Web Part in your SharePoint website you have to add the Web Part to the site collection Web Parts by going to the root site and then Site Actions >> Site Settings and then under Galleries go to Web Parts then click on “New” button, then you have to look for your Web Part which is have a name MyWebPart.Hello select it and then click on “Populate Gallery”. After that go and edit a page and try to add web part on the page you will see the web part “Hello” in the list of web parts under miscellaneous you can add it. 
After adding the web part you can see the html text which web write it in the render method so you can add any html controls or client side scripts (javascript/vbscript).
There are two views to the mysite, the first one for the public view for the anonymous or the other users and the other one for the private view for the same user.
To change the public view mysite you can change it by SharePoint Designer and open the website for the mysite, which is you create it and put it in “My Site Location URL” when you create the Shared Service Provider, and after open the website you find person.aspx page, which is the main page on the public view you can change that page also you change the master page under _catalgo/masterpage folders.
For the private view the master page it is under this folder “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL” you will find default.master you can change it or create new one and then go to the page under folder “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\SPSPERS” and you will find default.aspx page that is the main page for the private view for mysite you can change the layout and the masterpage url if you create another masterpage.
After doing the changes on the private view you have to restart IIS after that the changes will apply.
To Backup the portal web site which contains everything except users profile and my site and search index database
Stsadmn –o backup –url http://servername –filename c:\folder\file.bak –overwrite
The filename path it can be shared folder on the network like \\server\folder\file.bak and System Administrator should have modify permission on it. This file will contains everything on the portal the pages and all documents and permissions and the SharePoint groups, and this backup is most important and you should make it daily
To backup the users profiles and all Shared Services Providers which contains profiles settings and search settings just you need to backup the database for the SSP, the defualt name is “SharedServices1_DB” This backup it’s also important but it’s not really changed too much, so I think every week is enough.
To backup mysite for all the users you have to backup the whole web application “SharePoint - 80” which is contains all site under port 80 it means the portal websites and mysite. To do this just use this command lind
Stsadmn –o backup –directory c:\backupfolder –backupmethod full –item “SharePoint - 80”
After running this command line the backup folder will contains lots a .bak files and this folder can be a shared folder in the network and System Administrator should have modify permission on that folder This backup not really important and I prefer to do it weekly or every two week because it will do the first backup plus my site changes, unless the my sites is important for you but if you want to do this backup daily or with same time with the first backup command line no need to do that one.
The calendar appointment email is just normal email attached with ics file contains appointment details. This code will show you how to create simple ics file and how to attach it with the email. SmtpClient sc = new SmtpClient(); MailMessage msg = new MailMessage(); msg.From = new MailAddress("ahmed_dagga_84@hotmail.com", "Ahmed Abu Dagga"); msg.To.Add(new MailAddress("youremail@host.com", "Your Name")); msg.Subject = "Send Calendar Appointment Email"; msg.Body = "Here is the Body Content";
StringBuilder str = new StringBuilder(); str.AppendLine("BEGIN:VCALENDAR"); str.AppendLine("PRODID:-//Ahmed Abu Dagga Blog"); str.AppendLine("VERSION:2.0"); str.AppendLine("METHOD:REQUEST"); str.AppendLine("BEGIN:VEVENT"); str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime)); str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow)); str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime)); str.AppendLine("LOCATION: Dubai"); str.AppendLine(string.Format("UID:{0}", Guid.NewGuid())); str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body)); str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body)); str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject)); str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));
str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));
str.AppendLine("BEGIN:VALARM"); str.AppendLine("TRIGGER:-PT15M"); str.AppendLine("ACTION:DISPLAY"); str.AppendLine("DESCRIPTION:Reminder"); str.AppendLine("END:VALARM"); str.AppendLine("END:VEVENT"); str.AppendLine("END:VCALENDAR"); System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar"); ct.Parameters.Add("method", "REQUEST"); AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct); msg.AlternateViews.Add(avCal);
sc.Send(msg); The calendar text lines limit is 76 character if the body or any line more than that you have to split it to more lines the first line must be 76 character and the others is 75 with tab space in the begin of the line "\t" in C# and vbTab in VB. I hope the code is clear and If you have any question just add a comment in this blog.
|