Monday, August 22, 2011

Fetching All Replies/Messages of a DISCUSSION List/TOPIC

string ite = string.Empty;
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Emails"];
//Get the topic. (you can use other ways to get the topic)
SPFolder t = list.GetItemById(9).Folder;
SPQuery q = new SPQuery();
q.Folder = t;
SPListItemCollection res = list.GetItems(q);
foreach (SPListItem item1 in res)
{

object ss = item1["Body"];

object s1 = item1["EmailFrom"];
object s2 = item1["Modified"];


ite += " " + s1 + " " + ss + " " + s2.ToString() ;
for (int i = 0; i < item1.Attachments.Count; i++)
{
Response.Write("Attachment Url " + item1.Attachments.UrlPrefix + item1.Attachments + "
");
}



// Read body of attachment

Response.Write("Body: " + item1.Fields["Body"].GetFieldValueAsText(item1["Body"]) + "");
}
Response.Write(ite);

Monday, August 8, 2011

All Shorting Techniques

 
Save this code as sort.hpp 
 
 
// Sort.hpp - Implementation of Slow Sort method algorithm such as:
//            Insertion Sort, Bubble Sort, Selection Sort, Shell Sort
//            Radix Sort, Heap Sort, Merge Sort only.

#ifndef __J2K__SORT_HPP__
#define __J2K__SORT_HPP__    // To avoid multiple declaration of a class

#include "Const.hpp"
#include "MaxHeap.hpp"

static long POW2[17] = {   1,    2,     4,     8,    16,   
                                32,    64,   128,   256,  
                               512,  1024,  2048,  4096, 
                              8192, 16384, 32768, 65536  };

#define BITS  6

void InsertionSort(  Elem* array, int n  );
void BubbleSort(     Elem* array, int n  );
void SelectionSort(  Elem* array, int n  );
void ShellSort(      Elem* array, int n  );
void InsertionSort(  Elem* array, int n, int incr );

void MergeSort(Elem* array, int Size);
void HeapSort( Elem* array, int Size );
void RadixSort(Elem* array, long Size);

void QuickSort( Elem* array, int Size );
void BrightQSort(  Elem* array, ulong Size );
void OldQuickSort( Elem* array, ulong Size );

void MergeSort(Elem* array, Elem* temp, int left, int right);

struct QEntry {
   long Left;
   long Right;
};

// Swap longents: A[i] <==> A[j]
inline void swap( Elem* A, int i, int j ) 
{
  Elem temp = A[i];
  A[i] = A[j];
  A[j] = temp;
}

#endif
 


Source Code of the file for sorting

// Sort.cpp - Implementation of Slow Sort method algorithm such as:
//            Insertion Sort, Bubble Sort, Selection Sort, Shell Sort
//            Radix Sort, Heap Sort, Merge Sort only.

#include "Sort.hpp"

#define MSTHRESHOLD  0            // Declare the ThresHold for MergeSort

// Insertion Sort Algorithm
void InsertionSort( Elem* array, int n )
{
  // Insert i'th record on the top sorted part of the array
  for (int i = 1; i < n; i++)
    for (int j = i; (j > 0) && ( array[j] < array[j-1] ); j--)
      swap( array, j, j-1 );
}

// Bubble Sort Algorithm
void BubbleSort( Elem* array, int n )
{
  n--;  // n-1 optimization

  // Bubble up i'th record
  for (int i = 0; i < n; i++)
    for (int j = n; j > i; j--)
      if ( array[j] < array[j-1] )
        swap( array, j, j-1 );
}

// Selection Sort Algorithm
void SelectionSort( Elem* array, int n )
{
  n--;  // n-1 optimization

  // Select i'th record
  for (int i = 0; i < n; i++) 
  {
    int lowindex = i;                    // Remember its index
    for (int j = n; j > i; j--)          // Find the least value
      if ( array[j] < array[lowindex] )
         lowindex = j;                   // Put it in place

    swap( array, i, lowindex );
  }
}

// Modified version of Insertion Sort for varying increments
void InsertionSort( Elem* array, int n, int incr ) 
{
  for (int i = incr; i < n; i += incr)
    for (int j = i; ( j >= incr ) && ( array[j] < array[j-incr] ); j -= incr )
      swap( array, j, j-incr );
}

// Shell Sort Algorithm
void ShellSort( Elem* array, int n ) 
{
  for (int i = n / 2; i > 2; i /= 2)   // For each increment
    for (int j = 0; j < i; j++)        // Sort each sublist
      InsertionSort( &array[j], n-j, i);

  InsertionSort( array, n );
}

void MergeSort(Elem* array, int Size) {
  Elem* temp = new Elem[ Size ];
  MergeSort(array, temp, 0, Size-1);

  if ( temp != NULL ) {
       delete[] temp;
  }
}

void MergeSort(Elem* array, Elem* temp, int left, int right) {
  int i, j, k;
  int mid = (left+right) / 2;

  if (left == right) return;

  if ( (mid-left) < MSTHRESHOLD ) {
    SelectionSort( &array[left], mid-left);
  } else {
    MergeSort( array, temp, left, mid );   // MergeSort first half
  }

  if ( (right-mid-1) < MSTHRESHOLD ) {
    SelectionSort( &array[mid+1], right-mid-1 );
  } else {
    MergeSort(array, temp, mid+1, right);  // MergeSort second half
  }

  // Do the merge operation.  First, copy 2 halves to temp.

  for ( i = mid; i >= left; i--) {
    temp[i] = array[i];
  }

  for ( j = 1; j <= right-mid; j++) {
    temp[right-j+1] = array[j+mid];
  }

  // Merge sublists back to array
  for ( i = left, j = right, k = left; k <= right; k++) {
     if (temp[i] < temp[j]) {
        array[k] = temp[i++];
     } else {
        array[k] = temp[j--];
     }
  } 
}

void HeapSort( Elem* array, int Size )
{
  MaxHeap* H = new MaxHeap( array, Size, Size );   // Build the maxheap

  for (int i = 0; i < Size; i++) {   // Now sort by removing 
    H->removemax();                  // the max value placed at end of heap
  }
  
  delete H;
}

void RadixSort(Elem* array, long Size) {
  // Count[i] stores number of records in bin[i]

  Elem* count = new Elem[Size];
  
  Elem* A = array;
  Elem* B = new Elem[ Size ];
  long  n = Size;
  int   k = 16 / BITS;
  int   r = POW2[BITS];

  int i = 0;
  int j = 0;
  int rtok = 1;

  for ( i = 0, rtok = 1; i < k; i++, rtok *= r) {  // For k digits

    for ( j = 0; j < r; j++) {                     // Initialize count
       count[j] = 0;         
    }

    // Count the number of records for each bin on this pass
    for ( j = 0; j < n; j++ ) {
       count[ ( A[j] / rtok ) % r]++;
    }

    // Index B: count[j] will be index for last slot of bin j.
    for ( j = 1; j < r; j++ ) {
       count[j] = count[j-1] + count[j];
    }

    // Put records into bins working from bottom of each bin.
    // Since bins fill from bottom, j counts downwards
    for ( j = n-1; j >= 0; j--) {
       B[ --count[ ( A[j] / rtok ) % r ] ] = A[j];
    }

    for ( j = 0; j < n; j++ ) {  // Copy B back to A
       A[j] = B[j];         
    }
  }

  if ( B != NULL ) {
      delete[] B;
  }

  if ( count != NULL ) {
      delete[] count;
  }
}

REST web services

WEB SERVICES IN SHREPOINT

Rest Web Service can be used for

1: To get the List Items: This is possible by using REST web service of SharePoint server. We can get all the list items in xml by giving list name as parameter in the URL.

Syntax: http ://< site name>/_vti_bin/listdata.svc/

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise

2: To get the List in Sorted Order: List items can be sorted by using the following syntax: http:///_vti_bin/listdata.svc/?$orderby=Title desc, Name asc (returns List with Title in descending order and Name in ascending)

Sorting with “$orderby” keyword

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise?$orderby=Title asc, Discipline desc

3: To Filter list item: List items can be filtered by using the following Syntax: http:///_vti_bin/listdata.svc/?$filter=Title eq 'Title_Name' (returns List with Title = 'Title_Name')

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise?$filter=Title eq 'soft'

Filtering with “$filter” keyword in URL/Query string.

4: To retrieve items on page wise:

Syntax: http ://< site name>/_vti_bin/listdata.svc/? $skip=[n]&$top=[n]&$orderby=Title

The $skip and $top parameters are ideal for implementing a paging mechanism. The $skip=[n] parameter allows you to skip the first n options, the $top=[n] parameter allows you to return the next n items. Typically, you will use this in conjunction with the $orderby parameter (appended using the & (ampersand) symbol).

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise? $skip=5&$top=1&$orderby=Title

5: To select specific column:

Syntax: http:///_vti_bin/listdata.svc/?$ select = ,,..

EX: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise?$select = Title

Resource: http://www.lcbridge.nl/vision/2010/rest.htm

6: To limit on number of rows returned – In SharePoint, Site Collection Administrator can set the limit of number of rows returned & also programmers can override this value as per their requirements.

Resource: 1: http://msdn.microsoft.com/en-us/library/gg985387.aspx

2: https://willhlaw.wordpress.com/tag/sharepoint-2010/

Consuming REST Service with Linq in Visual Studio Application:

1) To consume a Restful web service in Visual Studio, you can go to the Data menu and click Add New Data Source.
2) In the Add Service Reference dialog, add the Url to a SharePoint 2010 web site.
3) Go to the Data menu again and this time, select Show data sources and select List
4) Add Reference

Using RestDemo.ServiceReference1;

Using System.Net;

Create a new class-level variable to hold the data context

TeamSiteDataContext context = new TeamSiteDataContext (new Uri(http://sp2010a/teamsite/_vti_bin/listdata.svc));

5) Next, in the Form_Load event, add the following two lines.

context.Credentials = CredentialCache.DefaultCredentials;

contactsBindingSource.DataSource = context.Contacts;

The first line will authenticate to SharePoint using your current logged-in account. Of course, other login methods are available. The second references the Contacts entity. The contactsBindingSource is an object automatically created by Visual Studio when you added the Contacts list to your form. The data source for the GridView automatically points to this data source.

We’ve seen above that REST allows you filter and sort. What if you wanted to be more efficient and filter and sort in application? You can and one way you can do this is with LINQ:

var q = from c in context.Contacts

where c.ID > 100

orderby c.FirstName

select new { c.FirstName, c.LastName, c.EMailAddress, c.BusinessPhone };

contactsBindingSource.DataSource = q;

Create User Profiles using SharePoint Object Model

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using System.Web;
using Microsoft.SharePoint.WebControls;
using System.Web.Profile;
using Microsoft.SharePoint.Portal.WebControls;

namespace add_user.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{

public string AccountName { get; set; }

public string WorkPhone { get; set; }


public string Department { get; set; }

public string Title { get; set; }

public string DistinguishedName { get; set; }

public string WorkEmail { get; set; }

public string Office { get; set; }

protected void Button1_Click(object sender, EventArgs e)
{

Create();
}

public void Create()
{

string socialDataStatsSite = SPContext.Current.Site.Url;

using (SPSite siteColl = new SPSite(socialDataStatsSite))
{
AccountName = "alok";
WorkPhone="9886178862";
Title="Mr";
DistinguishedName="alok";
WorkEmail="alok@pointcross.com";
Office = "Pointcorss";
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);

UserProfileManager userProfileManager = new UserProfileManager(serviceContext);

UserProfile newProfile = userProfileManager.CreateUserProfile(AccountName, AccountName);

newProfile[PropertyConstants.WorkPhone].Add(WorkPhone);

newProfile[PropertyConstants.Department].Add(Department);

newProfile[PropertyConstants.Title].Add(Title);

newProfile[PropertyConstants.DistinguishedName].Add(AccountName);

newProfile[PropertyConstants.Office].Add(Office);

newProfile.Commit();

}

}


}
}

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...