Wednesday, May 12, 2010

Sharepoint FBA- Login back to same page

To login back to the same page from wher eyou got logged out from - Sharepoint FBA


Uri MyLastURL;
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)

{

MyLastURL = Request.UrlReferrer;

ViewState["LastURL"] = MyLastURL.AbsoluteUri;
}
}

protected void Login_Click(object sender, EventArgs e)

{ //After successful authentication do Response.Redirect

Response.Redirect(ViewState["LastURL"]);

}

Tuesday, May 11, 2010

Rest App Pool with out doing IISRESET

Tips : #1 : Rest App Pool with out doing IISRESET can be donw with the help of :

cscript c:\windows\system32\iisapp.vbs /a "SharePointAppPool" /r


eg : cscript c:\windows\system32\iisapp.vbs /a "SharePoint - 222" /r


Tips : #2 : Generate Public Key Token for the assembly

 Open  Visual Studio 2005/ 2008 - At Tools - External Tools -  Select -Add.




Give  TitleGet Public Key Token

Add Command :  Browse to  Drive:\ProgramFiles\Microsoft Visual Studio 8.0\SDK\v2.0\Bin\sn.exe.

Add Arguments -Tp "$(TargetPath)".

Wednesday, May 5, 2010

Create document library groupings based on calculated column

Hi,

 I saw the above query resolved in a very easy manner by my team mate which i though of adding here so that it can help some one else and is a common requirement.

So, the question is how we can create groupings based on calculated column in a document library where the grouping should be in in bunches of  '50'.

So the easiest solution for acheive the same is to create a calculated column, add the below formula, and to create a grouping based on the calculated field.

=INT(REPLACE(GROUPFIELD,1,2,"")/50,1)

*assuming the field name as GROUPFIELD  which is a single line of text

Forums Link : http://social.technet.microsoft.com/Forums/en-US/sharepointgeneral/thread/81e0df1b-1945-4fdb-a2b7-ab8d597df439

Friday, April 23, 2010

Batch Process- MetaData update

Hi All,
My friend came up with a requirement to autoupdate metadata fields in a document library when proceeding with a bulk upload activity.
Requirements:

  • Update multiple metadata fields.
  • Fast process (If we use sharepoint DOM it wont be very fast- takes 2-3 minutes and increases as the no of items increase).
  • No new versions should be created on updation 
So i tried with  using Batchprocess which i feel is the fastest of all other methods.  
In the example here im updating two fields Details, and Approved which i am updating using batch process,
The field Approved is a Yes/No field with default value of 'No'.
So i m updating the Details field with 'Update description and time' and the Approved field to 'Yes'.

Note : I'm retreiving and updating the - Created, Created By, Modified, Modifed By - as i couldnt find a way to retain the version as we do in object model coding using SystemUpdate() instead of Update().
  
Code :-

 StringBuilder batchprocessbuilder = new StringBuilder(); 
string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + try{
"<ows:Batch OnError=\"Return\">{0}</ows:Batch>";

string methodFormat = "<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"ID\">{2}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Approved\">{3}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Details\">{4}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Created\">{5}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Created_x0020_By\">{6}</SetVar>"
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Modified\">{7}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Modified_x0020_By\">{8}</SetVar>" +"</Method>";

string querystring = "<Where><And><Eq><FieldRef Name='Approved' />"+
"<Value Type='Boolean'>No</Value></Eq><Eq><FieldRef Name='Created' />" +
"<Value Type='DateTime'>{0}</Value></Eq></And></Where>";


 using (SPWeb currentweb = SPContext.Current.Web){

 SPList currentlist = currentweb.Lists["DocLib"];

 //just to make sure no versioning is happening. currentlist.EnableVersioning = false;
currentlist.Update();
string listID = currentlist.ID.ToString();
SPQuery query = new SPQuery();
string datetoday = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now);
query.Query = string.Format(querystring, datetoday);
query.ViewAttributes = "Scope= 'Recursive'";
SPListItemCollection listcollectn = currentlist.GetItems(query);
//fileds updating........

int boolApproved = 1;
string strDetails = "UpdateMetaData" +Convert.ToString(DateTime.Now);

SPListItem item = null;
//Use only for loop, foreach will hang a lot....
for(int i=0; i<listcollectn.Count; i++){
int itemID = listcollectn[i].ID;
item = listcollectn.GetItemById(itemID);
DateTime itemdate = Convert.ToDateTime(item["Created"].ToString());
string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(itemdate);
batchprocessbuilder.AppendFormat(methodFormat, itemID, listID, itemID, boolApproved, strDetails, date, item["Author"], date, item["Author"]);}
string batch = string.Empty;
batch = string.Format(batchFormat, batchprocessbuilder.ToString());
string batchresults = currentweb.ProcessBatchData(batch);
currentlist.EnableVersioning = true;
currentlist.Update();}
}}catch (Exception ex){}

Method : 2 : Using Webservice and Jquery
ref: http://www.sharepointresource.com/category/jQuery.aspx
http://weblogs.asp.net/jan/archive/2009/05/06/querying-sharepoint-list-items-using-jquery.aspx


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:soap="http://schemas.microsoft.com/sharepoint/soap/">
<soapenv:Header />
<soapenv:Body>
<soap:UpdateListItems>
<soap:listName>Contacts</soap:listName>
<soap:updates>
<Batch OnError='Continue' ListVersion='1'>
<Method ID='2' Cmd='Update'>
<Field Name='ID'>4</Field>
<Field Name='Title'>Last_Name</Field>
<Field Name='FirstName'>First_Name</Field>
</Method>
</Batch>
</soap:updates>
</soap:UpdateListItems>
</soapenv:Body>
</soapenv:Envelope>

We can use :
Create: <Method ID='1' Cmd='New'>
Update: <Method ID='2' Cmd='Update'>
Delete: <Method ID='3' Cmd='Delete'>





Wednesday, March 10, 2010

Archive SharePoint Document Library versions.

Requirement : Archive versions created in a document library to another document library on daily basis.

Study :  There are vairuos option to try doing it, but to automate the process its better to go fir shareoint timer jobs.

Solution : Create timer jobs which starts on feature activation performing the requirement on daily basis.

  1.  Create Timer job in a feature.
         You can start creating a timer job and attaching it with feature from the below site steps.
           http://www.andrewconnell.com/blog/archive/2007/01/10/5704.aspx
           http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx

        Only difference from the example is that you have to use SPDailySchedule
         http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spdailyschedule.aspx

   2.  Write code for perform versioning in the document library

       public override void Execute (Guid contentDbId) {
       try
           {   
             string[] urlofFile = null;
             string url = string.Empty;
             byte[] dBytes = null;
             StringBuilder verID = new StringBuilder(10);
             SPFileVersionCollection vercol = null;
             SPFile file = null;
         //Call webapplication
                   SPWebApplication webApplication = this.Parent as SPWebApplication;
                  SPContentDatabase contentDataBase = webApplication.ContentDatabases  [contentDBGuid];
          //call document librarys
   SPDocumentLibrary doclib1 = (SPDocumentLibrary)contentDataBase.Sites[0].RootWeb.Lists ["DocLib"];
   //Archival doc library
SPDocumentLibrary doclib2 = (SPDocumentLibrary)contentDataBase.Sites[0].RootWeb.Lists["Archives"];
//get current web
SPWeb currentweb = contentDataBase.Sites[0].RootWeb;
//get filecollection.
SPFileCollection filecols = currentweb.GetFolder("/DocLib").Files;
//Call Spfolder to add items to the archive.
SPFolder doclib2Folder = currentweb.Folders["/Archives"];
foreach (SPListItem items in doclib.Items){
//file object to get the files stored in doclib
file = items.File;
if (file.Exists){
string fileurl = file.Url.ToString();
//get collection of fileversions.
vercol = filecols[fileurl].Versions;
//Add the versions to Archive documents library if not a current version.
foreach (SPFileVersion ver in vercol){
if (!ver.IsCurrentVersion){
verID = verID.Append(ver.ID);
verID = verID.Append(",");
urlofFile = ver.File.Name.Split('.');
//Add version ID of the file version for uniqueness
url = urlofFile[0].ToString() + ver.ID + "." + urlofFile[1].ToString();
//get the versions of the file.
dBytes = ver.OpenBinary();
//Add it to Archive doclibrary
currentweb.AllowUnsafeUpdates = true;
doclib2Folder.Files.Add(url, dBytes);
doclib2.Update();
currentweb.AllowUnsafeUpdates = false;
}}
//Delete the versions from the current document library
if (!string.IsNullOrEmpty(verID.ToString())){
string[] vervalues = verID.ToString().TrimEnd(',').Split(',');
int i = 0;
foreach (string s in vervalues){
currentweb.AllowUnsafeUpdates = true;
i = Convert.ToInt32(s);
vercol.GetVersionFromID(i).Delete();
currentweb.AllowUnsafeUpdates = false;
}verID.Length = 0;
}}}}
catch (Exception ex)
{//Handle Exceptions....}
}

3. Deploy the solution as a feauture to your shareoint solution and activate the feature.

Happy Coding...........

Tuesday, March 9, 2010

Create Modify Views and columns In SharePoint Survey....

Had you ever tried creating a new view in sharepoint survey list ?


You won't find an option to do it by default but still you can do it  for a sharepoint survey.


Find the steps below >>   Create New View
  • Open the new view page of any list in your sharepoint site, and copy the URL to a notepad.
  • Open  survey settings page, and from the URL copy the List ID. (List=%7Bxxxxxxxx%2Dxxxx%2Dxxxx%2Dxxxx%2Dxxxxxxxxxxxx%7D).
  • Replace the list name to the last of the URL with the survey list name.
Copy the modified URL to a new browser window, and you are done, you will be now in the 'Create View' page of your Survey List. Create the view as you wish here..........


Find the steps below >> Create New Column.


  • Same the way you did for survey view, copy the new column page url (/_layouts/fldNew.aspx?ListID)
  • Replace the ListID with the survey list ID and you are done.
Find the steps below >> Modify View.


  • Inorder to perform the modify view of the newly created view as per the steps above, just add your list as a webpart to any sharepoint page.
  • In the modify shared webpart option, select the view which you want to edit in the Selected View and  select the option 'Edit the current view'.


Enjoy Customisation...........



Wednesday, December 23, 2009

Sharepoint Reports using Sharepoint Designer 2007

I was searching a lot for tools which can generate reports for me in sharepoint for many requirements like site usage, find the modified item in a document library, who all have modified a document in a document library, who had mad changes in the site..

The final answer on how to acheive all the same was simple.

Use Sharepoint Designer 2007

Yes, you can generate all the reports, create charts and even export them to excel from the sharepoint designer 2007. Open your site using sharepoint designer and at Site -> Reports,  you can find almost all the options you are looking for......

Find this how to Article with more details.