Finding individual utility There are N agents living in an economy with two goods, $X$ and $Y$. Their preferences are described by the following utility function $u(X,Y) = 2 \sqrt{XY}$. Each agent is endowed with 1 unit of $X$ and $y_{i}$ units of $Y$. Each unit of $Y$ is sells for $p$ units of $X$. 

The question is to show that each agent gets a utility of $\frac{1 + py_{i}}{\sqrt p}$.

Here is what I have tried:

The agent chooses $X,Y$ in order to maximise $2\sqrt{XY}$ subject to $X + pY = 1 + p y_{i}$ So I have expressed the resource constraint in units of good $X$. The LHS is what the agent can buy and the RHS is the endowment, also expressed in units of $X$.

I then re-arrange the constraint in terms of $Y$, i.e, $Y = \frac{1}{p} + y_{i} - \frac{X}{p}$ and substitute this in to the utility function, with the FOC with respect to $X$ and then substitute the optimal value of $X$ back in to the utility function to get an expression in terms of $y_{i}$. However, the expression is not what I am supposed to get.

If anyone wants to check my algebra, here it is:

The utility function becomes $2\sqrt {(X/p) + Xy_{i} - X^2/p}$ Differentiating this w.r.t $X$ I find that $X^* = (1/2)(1 + y_{i}p)$. I substitute this in the utility function, $2\sqrt{X^*y_{i}}$ but this does not equal $\frac{1 + py_{i}}{\sqrt p}$.

Any ideas on where I am going wrong?
Your mistake probably occurred at the final substitution stage. Optimal utility should be $2\sqrt{x^*y^*}$, which is not the same as $2\sqrt{x^*y_i}$.

In general, maximizing a two-good Cobb-Douglas utility function $u(x,y)=Ax^\alpha y^\beta$ subject to budget constraint $p_xx+p_yy=m$ has general solution 
\begin{equation}
x^*=\frac{\alpha}{\alpha+\beta}\frac{m}{p_1}
\quad\text{and}\quad 
y^*=\frac{\beta}{\alpha+\beta}\frac{m}{p_2}.
\end{equation}

In your case, the parameters $A,\alpha,\beta,p_x,p_y,m$ take the following values: 
\begin{equation}
A=2,\quad\alpha=\beta=\frac12,\quad p_x=1,\quad p_y=p,\quad m=1+py_i.
\end{equation}
Plugging in the values, you get optimal consumption:
\begin{equation}
x^*=\frac{1+py_i}{2},\quad y^*=\frac{1+py_i}{2p}.
\end{equation}
Hence optimal utility is 
\begin{equation}
u(x^*,y^*)=2\sqrt{\frac{1+py_i}{2}\cdot \frac{1+py_i}{2p}}=\frac{1+py_i}{\sqrt p}
\end{equation}