Tuesday, April 5, 2011

only allow assigned user to edit

SPListItem item = properties.ListItem;
SPWeb site = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl);
SPUser CurrentUser = site.AllUsers[properties.UserLoginName];
int index = item["AssignedTo"].ToString().IndexOf(';');
int id = Int32.Parse(item["AssignedTo"].ToString().Substring(0, index));
SPUser AssignedUser = site.SiteUsers.GetByID(id);
bool okUpdate = (AssignedUser.LoginName.Equals(CurrentUser.LoginName) || properties.UserDisplayName.Equals("System Account") || CurrentUser.IsSiteAdmin);
if (!okUpdate)
{
properties.Cancel =true;
properties.ErrorMessage ="You are not authorized to edit the task.";
return;
}

***Note:I found it from a site while searching in google and it helped in my work also.But,I do not remember the author name.It is very helpful.That's why I published it.

A java script in content editor webpart can make a slideshow webpart.











*Note: I found it from a blog in NET and posted here to share it for service purpose only.

How to Deploy a Custom WebPart in SharePoint 2007 server?


Create a Visual Studio Project-àGo to C# and the select Class Library Project-àName the Project and press OKàrename the class 1.cs to a usefulname.csàAdd a reference and browse Microsoft.SharePoint.dll from Drive: \Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\ Microsoft.SharePoint.dllàAdd reference to System. Web and System. Dataàgo to class file (.cs file) and add the following namespaces there.
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Data;
Then go to class and take an inheritance from System.Web.UI.WebControls.WebParts.WebPart   like the following.
namespace MyFirstWP
{
publicclass MyFirstWebPart:System.Web.UI.WebControls.WebParts.WebPart
    {
publicMyFirstWebPart()
        {
        }

Label lbl = null;
      //TextBox tb = null;
protectedoverridevoid CreateChildControls()
        {
            lbl = newLabel();
            lbl.Text = "Hi, WelCome To Bangalore!";
this.Controls.Add(lbl);
           // tb = newTextBox();
           // tb.Text = "Hi, Fourth Experiment done Successfully.";
           // this.Controls.Add(tb);
base.CreateChildControls();
        }

    }
}
Go to Project Property and give strong name and save it. Now build the Project and see the Output. Go to the Project‘s physical Path and find the .dll file (MyFirstWP.dll) under bin\debug     and drag the dll file to  \Windows\assembly and note the PublicKeyToken  and version of the file for future use.
Deployment Process:
It can be deployed through two ways.
(i)     Through bin folder deployment on the WebApplication.
(ii)   wsp deployment

(i)Bin folder deployment:
Go to Drive:\Inetpub\wwwroot\wss\VirtualDirectories\WebApplication Port no.\bin and drag the dll file (MyFirstWP.dll) from project folder to here. And save it.
Now go to the web.config  file (:\Inetpub\wwwroot\wss\VirtualDirectories\WebApplication Port no.\web.config) and add a SafeControl for the webpart like the following.
<SafeControlAssembly=" MyFirstWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=37a0ec882203f23c"Namespace=" MyFirstWP "TypeName="*"Safe="True" />
Now save the config file.
Now go the SiteCollection and click on Site ActionsàSite SettingsàWebPartsàWebPart Galleryàclick on Newàfind  MyFirstWP.MyFirstWebPart under Web Part Type Name and click the checkbox and click on Populate GalleryàNow go to the site àclick on Site ActionsàEdit Pageàadd the webpartàFind it in Miscellaneous or in group if you named itàNow click on Exit Edit Modeand see the webpart on the site.
(ii)WSP DEPLOYMENT:
For WSP deployment we need manifest.xml file and have to create a CAB Project.
Two ways we can do it. One by feature and another without feature. We can use WSPBuilder also to deploy.

1.(1.1)Add a manifest.xml file to the Project and write like following.

<?xmlversion="1.0"encoding="utf-8" ?>
<Solutionxmlns="http://schemas.microsoft.com/sharepoint/"

SolutionId="6D661BB0-CCAF-4e27-AC37-9C72E11D6304">

<Assemblies>

<AssemblyDeploymentTarget="WebApplication"

Location="MyFirstWP.dll">

<SafeControls>

<SafeControl

Assembly=" MyFirstWP, Version=1.0.0.0, Culture=neutral,PublicKeyToken=37a0ec882203f23c"
Namespace="MyFirstWP"
TypeName="*"/>

</SafeControls>

</Assembly>

</Assemblies>

</Solution>


(1.2)Now, Go to the SolutionExplorer and right click on Solution and add a new Project.Go to Other Project Types and there click on Setup and Deployment. Then Click on CAB Project and name it (like MyFirstWPDeployment)and press OK.
Now, go to the newly created Project folder (MyFirstWPDeployment) on solution explorer and right click on it and click on àADDàProject Output àclick on Primary Outputàpress OK.
Now, again right click on the Project (MyFirstWPDeployment) on solution explorer and right click on it and click on àADDàProject Outputàclick on Content Filesà press OK.
Now Build the Project.

Now go to the Project(MyFirstWPDeployment) Property and rename the cab file name fro .CAB to .wsp.
See below.
Go to the Projectàright click on it and click on Project Propertyàgo to Output file nameà there change file name from .CAB to .wsp like Debug\MyFirstDeployment.wspàpress OK.

Now go to the physical path of the folder and change the file name from MyFirstDeployment.CAB to MyFirstDeployment.wsp and refresh once.

Now, build the solution once. (It is not needed.)

Now, our wsp file is ready and we can deploy it on SharePoint server. See below.

(1.3)Go to Visual Studio 2008 Command Prompt and enter like following.
 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Bin>stsadm – o –addsolution             –filename  “Physical path of .wsp file”

Now deploy the solution
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Bin>stsadm –o - deploysolution          –name  “filename.wsp ” –url “WebApplicationurl” –immediate

Now go the SiteCollection and click on Site ActionsàSite SettingsàWebPartsàWebPart Galleryàclick on Newàfind  MyFirstWP.MyFirstWebPart under Web Part Type Name and click the checkbox and click on Populate GalleryàNow go to the site àclick on Site ActionsàEdit Pageàadd the webpartàFind it in Miscellaneous or in group if you named itàNow click on Exit Edit Modeand see the webpart on the site

2.Deployment With Feature.

(2.1)Right click on the Project File(MyFirstWP) and add a new folder name it MyFirstWebPart.Add three xml files on it and name these as feature.xml, MyFirstWebPart.xml and MyFirstWebPart.webpart respectively.The files would be like the following.

feature.xml:

<?xmlversion="1.0"encoding="utf-8" ?>
<Featurexmlns="http://schemas.microsoft.com/sharepoint/"
Id="6E814BFB-CECA-4ab1-A585-3081099E8CBA"
Title="MyFirstWP"
Description=" MyFirstWP description."
Scope="Site"
> 

<ElementManifests>
<ElementManifestLocation=" MyFirstWebPart.xml "/>
</ElementManifests>

</Feature>




MyFirstWebPart.xml:

<?xmlversion="1.0"encoding="utf-8" ?>
<Elementsxmlns="http://schemas.microsoft.com/sharepoint/">
<ModuleName="WebParts"List="113"Url="_catalogs/wp">
<FileUrl="MyFirstWebPart.webpart"Type="GhostableInLibrary">
<PropertyName="Group"Value="MyFirstWP"></Property>
</File>
</Module>
</Elements>

MyFirstWebPart.webpart:

<?xmlversion="1.0"encoding="utf-8" ?>
<webParts>
<webPartxmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<typename="MyFirstWP.MyFirstWebPart, MyFirstWP,Version=1.0.0.0,Culture=neutral,PublicKeyToken=37a0ec882203f23c" />
<importErrorMessage>Cannot import this web part.</importErrorMessage>
</metaData>
<data>
<properties>
<propertyname="Title"type="string">MyFirstWebPart</property>
<propertyname="Description"type="string">MyFirstWP MyFirstWebPart description.</property>
</properties>
</data>
</webPart>
</webParts>

(2.2)Right click on the Project (MyFirstWP) add a xml file and name it manifest.xml.

Just follow (1.1).

manifest.xml:

<?xmlversion="1.0"encoding="utf-8" ?>
<Solutionxmlns="http://schemas.microsoft.com/sharepoint/"

SolutionId="6D661BB0-CCAF-4e27-AC37-9C72E11D6304">
<Assemblies>
<AssemblyDeploymentTarget="WebApplication"
Location="MyFirstWP.dll">
<SafeControls>
<SafeControl
Assembly=" MyFirstWP, Version=1.0.0.0, Culture=neutral,PublicKeyToken=37a0ec882203f23c"
Namespace="MyFirstWP"
TypeName="*"/>
</SafeControls>
</Assembly>
</Assemblies>
</Solution>


(2.3)Now, follow (1.2)to create a wsp file.

(2.4) Go to Visual Studio 2008 Command Prompt and enter like following.
 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Bin>stsadm –o - addsolution            –filename  “Physical path of .wsp file”

Now deploy the solution
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Bin>stsadm –o - deploysolution         –name  “filename.wsp ” –url “WebApplicationurl” –immediate

Install  the feature:

First  go to the physical path of the project folder and find the folder MyFirstWebPart which contains
feature.xml ,MyFirstWebPart.xml,MyFirstWebPart.webpart  files. Copy the folder and paste on
\12\TEMPLATE\FEATURES and refresh the 12 hive.


Now do like following on Visual Studio 2008 Command Prompt

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Bin>stsadm –o  –installfeature            –filename “MyFirstWebPart\feature.xml” –force


Activate the Feature:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Bin>stsadm –o –activatefeature    –filename “MyFirstWebPart\feature.xml” –url ”WebApplicationurl”

(2.5) Now go to the web.config  file (Drive:\Inetpub\wwwroot\wss\VirtualDirectories\WebApplication Port no.\web.config) and add a SafeControl for the webpart like the following.
<SafeControlAssembly=" MyFirstWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=37a0ec882203f23c"Namespace=" MyFirstWP "TypeName="*"Safe="True" />
Now save the config file.

Now go the SiteCollection and click on Site Actionsàclick on Site ActionsàEdit Pageàadd the webpartàFind it in Miscellaneous or in group if you named itàNow click on Exit Edit Mode and see the webpart on the site.