30 April 2011

Gotchas while Updating date field while restoring item from recycle bin Moss 2010

Recently I came across a scenario in which I need to restore a document which is send to recycle bin by a retention process. And I need to reset the date field that is reference for retention, so that it will again fall in the retention cycle.
So How should I know in the event receiver that the ‘ItemAdding” is happening when the item is newly added or item is being restored. I will know this by checking the ListitemID property inside the “ItemAdding” event , If ListItemId is non zero then it is fired from restore. But ‘ItemAdding” event will not allow you to change any property while the item is getting restored.
So I need to relay on the ‘Itemadded” Event. So here how should I check that The event is fired from restore or while ‘ItemAdding” initially. I checked the current date with the created date of the list item for this. If they are different then the event is being fired from the restore.(the business logic is that you need to set min 1 day retention period).And ‘Itemadded” event is the only place I can reset my datefield refered by the retention process.
I faced another issue here, if your server is in a different time zone than the client you cannot update this date field (need to use ISO format) with datetime.now. You need to use datetime.utcnow.
But if the server is in the same time zone You can either use DateTime.Nowor DateTime.UtcNowto update the date field.
So I done similar below to deploy the code in the server that reside in a different time zone.
public override void ItemAdded(SPItemEventProperties properties)
        {
                base.ItemAdded(properties);
                EventFiringEnabled = false;
                SPListItem currentItem = properties.ListItem;
                DateTime created = (DateTime)currentItem[CREATED];
                //Compare the Created Date and The Present datetime. If Created date is not same as current datetime, it means the docuemnt was restored.
               //While restoring retention date is set to the current utc date, so that it will fall back to recycle bin  in the next retention stage
                int recycle = DateTime.Compare(created.Date, System.DateTime.Now.Date);
                if (recycle != 0)
               { //Update The UploadedDate to Current Date Time
                    DateTime today = DateTime.UtcNow.Date;
                    Util.LogInfo("UTC", SPUtility.CreateISO8601DateTimeFromSystemDateTime(today));
                    currentItem[RET_REF_DATE_STAT] = SPUtility.CreateISO8601DateTimeFromSystemDateTime (today);
currentItem.Update();
}
 }

27 April 2011

Checking specific set of permission for current user

bool hasManagePermission = false;
 SPSecurity.RunWithElevatedPrivileges(delegate()
 {//how to check current user have a specific set of permission(s)
   //eg. here we are checking the user have ManagePermissions
    hasManagePermission = Web.DoesUserHavePermissions(SPBasePermissions.ManagePermissions);
 });

16 April 2011

Sending html tags to server

By encoding html by java script, we can send the html to the server with out setting ValidateRequest=”false”.
And in the server side we can just decode it.

11 April 2011

Schedule item in sharepoint library 2010

Item scheduling is a special facility in sharepoint 2010 which help to publish a document for a specified period in a publishing site.
Item scheduling can be implemented only on libraries. Versioning need to be set for the library for this.Publishing server feature need to be activated for this
Create a new content type derived from the document content type, while creating this content type add the Scheduling start date, and Scheduling end date SharePoint inbuilt custom column in the content type along with the other columns required.
Use the custom content type in the library.
Once published the document will be in the schedule.


9 April 2011

Publishing Service Applications sharepoint 2010

Why?
Optimizing resources and reducing redundancy are two of the main reasons you would publish a service application.
Which Services can be published?
Only the cross fram services can be published.
Following are the cross farm services in sharepoint 2010
Business Data Connectivity,Managed Metadata,People (User Profiles),Search,Secure Store,Web Analytics
What need to be done?
For a farm to consume a service application that is published by another farm, the following actions must be performed in the following order.
Administrators of both the publishing and consuming farms must exchange trust certificates.
An administrator of the consuming farm must provide two trust certificates to the publishing farm: a root certificate and a security token service (STS) certificate.

An administrator of the publishing farm must provide a root certificate to the consuming farm.
To establish trust on the consuming farm, you must import the root certificate that was copied from the publisher farm and create a trusted root authority.
On the farm on which the application resides, an administrator must explicitly publish the service application.
An administrator must connect the consuming farm to the service application.

Steps
on consuming farm
1.Export the root certificate from the consuming farm

$rootCert = (Get-SPCertificateAuthority).RootCertificate
$rootCert.Export("Cert") | Set-Content <C:\ConsumingFarmRoot.cer> -Encoding byte
2.Export the STS certificate from the consuming farm
$stsCert = (Get-SPSecurityTokenServiceConfig).LocalLoginProvider.SigningCertificate
$stsCert.Export("Cert") | Set-Content <C:\ConsumingFarmSTS.cer> -Encoding byte
on publishing farm
3.Export the root certificate from the publishing farm
$rootCert = (Get-SPCertificateAuthority).RootCertificate
$rootCert.Export("Cert") | Set-Content <C:\PublishingFarmRoot.cer> -Encoding byte

4.Import the root certificate and create a trusted root authority on the consuming farm
$trustCert = Get-PfxCertificate <C:\PublishingFarmRoot.cer>
New-SPTrustedRootAuthority <PublishingFarm> -Certificate $trustCert
5.Import the root certificate and create a trusted root authority on the publishing farm
$trustCert = Get-PfxCertificate <C:\ConsumingFarmRoot.cer>
New-SPTrustedRootAuthority <ConsumingFarm> -Certificate $trustCert

6.Import the STS certificate and create a trusted service token issuer on the publishing farm
$stsCert = Get-PfxCertificate <c:\ConsumingFarmSTS.cer>
New-SPTrustedServiceTokenIssuer <ConsumingFarm> -Certificate $stsCert

General Security
To establish trust by using Central Administration
Security>General Security>Manage trust

On the Trust Relationship page, on the ribbon, click New.
On the Establish Trust Relationship page:
Supply a name that describes the purpose of the trust relationship.

Browse to and select the Root Authority Certificate for the trust relationship.
(This must be the Root Authority Certificate that was exported from the other farm by using Windows PowerShell.

If you are performing this task on the publishing farm, select the check box for Provide Trust Relationship.
Type in a descriptive name for the token issuer and browse to and select the STS certificate that was copied from the consuming farm.
Click OK.

After a trust relationship is established, you can modify the Token Issuer description or the certificates that are used by clicking the trust, and then clicking Edit. You can delete a trust by clicking it, and then clicking Delete.