Monday 8 September 2014

SharePoint Repository Pattern Step By Step

This Post has been in my agenda for sometime but finally i got the time to write it:

I have done a lot of work with SharePoint API in my career path , and each time i see this code i become upset and feel angry even i  wrote this code many times :


             using (SPSite  site = new SPSite(path))
                {
                    using (SPWeb  web =  site.OpenWeb())
                    {
                          //Do work
                    }
              }


Problem:

Project  Solution (SPRepository)  will  consist of  three Projects:
  1. SPRepository.Core (class library): contains the Repository 
  2. SPRepository.Domin (class library) : Reference the (1) contains all entities 
  3. SPRepository.Portal (SharePoint Project). Reference  (1 and 2) and contains web parts(UI)




SPRepository.Core (class library): 


The class diagram for (SPRepository.Core) as below image (Figure1) 
Two weeks ago (today 3/10/2013) my manger asked me to make some changes on online Portal.
I got shocked on seeing hundreds of repeated codes.

I went to Google and start searching (sharepoint document repository).
But i didn’t find what i wants so i start reading and assembly parts to come up with complete solution that might help me and help others,
Of course i got some idea from other blogs.
Let's start talking about the Solution:


 Figure 1



Description of Class Diagram
class EntityBase
Is the base class of each entity  also please note that the entity represent SPList and contains the common Fields in SPList (ID , Title , Modified , Created, ModifiedBy , CreatedBy)


interface IMapper<TEntity>
Is the mapper that converts an entity of type (EntityBase) to and from SPListItem.
Its convert and SPListItem to EntityBase and vice versa


interface IRepository<TEntity>
contains all method that we need to work with SPList like Query , getting item and update item(SPListItem)


class Repository<TEntity> is the real implementation for IRepository<TEntity>

Let’s start by real example:

We have Department SPList with Fields
  1.    ID
  2.          Title
  3.           DeptCode

Also We have SPList called Employee with fields:
  1.                ID
  2.              Title
  3.               FirstName
  4.    LastName
  5.              Department (Lookup filed)





We will start by building Entity objects in the Project (SPRepository.Domin)
Add new class called Department inherited from Class EntityBase
and add Properties: (DeptCode , don't add other Properties because it's already in the base class EntityBase).



Add new class called Employee inherited from Class EntityBase
and add Properties:























 Okay don't upset it's very easy , we will start n example showing  step by step to how we can use the repository.




To be continue...

if you interested drop a comment











Friday 5 September 2014

ajax error syntaxerror json parse unexpected token Jsonresult

I want Load Subcategories based on selected category using JQuery Ajax or JSON , on asp.net MVC application. 

Problem:
I've upgraded to VS.NET 2013, and now, every time I receive error when getting subcategories for category .

Error:jqXHR: 200textStatus: parser errorerror Thrown: SyntaxError: Unexpected token 


some solutions on internet was changing dataType:json or remove dataType and removing the content Type .. ,  i tried every possible solution with no luck.


after 4 hours of trying to solve the problem .i created on page with 2 dropdownlists only without master page, when i open HTML source code i found this line of code.


<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"ac71e2a7594e4504a5135609483e8c7a"}
</script>
<script type="text/javascript" src="http://localhost:8157/eb9f5c3c339d488fb419a027dfa861bd/browserLink" async="async"></script>
<!-- End Browser Link -->



This is script is interfering with JQuery  library  and it crashes the jQuery reference.

this code came from new feature in visual studio 2013 called  Browser Link
After i disabled it the error went and every thing work as expected.





to disable it: